fix detection

pull/402/head
Pete Matsyburka 1 month ago
parent 71528eda45
commit daba2505bc

@ -484,6 +484,7 @@
:show-tour-start-form="showTourStartForm" :show-tour-start-form="showTourStartForm"
@add-field="addField" @add-field="addField"
@set-draw="[drawField = $event.field, drawOption = $event.option]" @set-draw="[drawField = $event.field, drawOption = $event.option]"
@select-submitter="selectedSubmitter = $event"
@set-draw-type="[drawFieldType = $event, showDrawField = true]" @set-draw-type="[drawFieldType = $event, showDrawField = true]"
@set-drag="dragField = $event" @set-drag="dragField = $event"
@set-drag-placeholder="$refs.dragPlaceholder.dragPlaceholder = $event" @set-drag-placeholder="$refs.dragPlaceholder.dragPlaceholder = $event"

@ -222,7 +222,16 @@
width="22" width="22"
class="animate-spin" class="animate-spin"
/> />
<span class="hidden md:inline"> <span
v-if="analyzingProgress"
class="hidden md:inline"
>
{{ Math.round(analyzingProgress * 100) }}% {{ t('analyzing_') }}
</span>
<span
v-else
class="hidden md:inline"
>
{{ fieldPagesLoaded }} / {{ numberOfPages }} {{ t('processing_') }} {{ fieldPagesLoaded }} / {{ numberOfPages }} {{ t('processing_') }}
</span> </span>
</template> </template>
@ -363,10 +372,11 @@ export default {
default: false default: false
} }
}, },
emits: ['add-field', 'set-draw', 'set-draw-type', 'set-drag', 'drag-end', 'scroll-to-area', 'change-submitter', 'set-drag-placeholder'], emits: ['add-field', 'set-draw', 'set-draw-type', 'set-drag', 'drag-end', 'scroll-to-area', 'change-submitter', 'set-drag-placeholder', 'select-submitter'],
data () { data () {
return { return {
fieldPagesLoaded: null, fieldPagesLoaded: null,
analyzingProgress: 0,
defaultFieldsSearch: '' defaultFieldsSearch: ''
} }
}, },
@ -448,8 +458,6 @@ export default {
while (true) { while (true) {
const { value, done } = await reader.read() const { value, done } = await reader.read()
if (done) break
buffer += decoder.decode(value, { stream: true }) buffer += decoder.decode(value, { stream: true })
const lines = buffer.split('\n\n') const lines = buffer.split('\n\n')
@ -464,10 +472,21 @@ export default {
if (data.error) { if (data.error) {
alert(data.error) alert(data.error)
this.template.fields = data.fields || fields
break break
} else if (data.analyzing) {
this.analyzingProgress = data.progress
} else if (data.completed) { } else if (data.completed) {
this.fieldPagesLoaded = null this.fieldPagesLoaded = null
this.template.fields = fields
if (data.submitters) {
this.template.submitters = data.submitters
this.$emit('select-submitter', this.template.submitters[0])
}
this.template.fields = data.fields || fields
this.save() this.save()
break break
@ -484,11 +503,14 @@ export default {
} }
} }
} }
if (done) break
} }
}).catch(error => { }).catch(error => {
console.error('Error in streaming message: ', error) console.error('Error in streaming message: ', error)
}).finally(() => { }).finally(() => {
this.fieldPagesLoaded = null this.fieldPagesLoaded = null
this.analyzingProgress = null
this.isFieldsLoading = false this.isFieldsLoading = false
}) })
}, },

@ -1,4 +1,5 @@
const en = { const en = {
analyzing_: 'Analyzing...',
download: 'Download', download: 'Download',
downloading_: 'Downloading...', downloading_: 'Downloading...',
view: 'View', view: 'View',
@ -187,6 +188,7 @@ const en = {
} }
const es = { const es = {
analyzing_: 'Analizando...',
download: 'Descargar', download: 'Descargar',
downloading_: 'Descargando...', downloading_: 'Descargando...',
view: 'Vista', view: 'Vista',
@ -374,6 +376,7 @@ const es = {
} }
const it = { const it = {
analyzing_: 'Analisi...',
download: 'Scarica', download: 'Scarica',
downloading_: 'Download in corso...', downloading_: 'Download in corso...',
view: 'Vista', view: 'Vista',
@ -561,6 +564,7 @@ const it = {
} }
const pt = { const pt = {
analyzing_: 'Analisando...',
download: 'Baixar', download: 'Baixar',
downloading_: 'Baixando...', downloading_: 'Baixando...',
view: 'Visualizar', view: 'Visualizar',
@ -748,6 +752,7 @@ const pt = {
} }
const fr = { const fr = {
analyzing_: 'Analyse...',
download: 'Télécharger', download: 'Télécharger',
downloading_: 'Téléchargement...', downloading_: 'Téléchargement...',
view: 'Voir', view: 'Voir',
@ -935,6 +940,7 @@ const fr = {
} }
const de = { const de = {
analyzing_: 'Analysiere...',
download: 'Download', download: 'Download',
downloading_: 'Download...', downloading_: 'Download...',
view: 'Anzeigen', view: 'Anzeigen',
@ -1122,6 +1128,7 @@ const de = {
} }
const nl = { const nl = {
analyzing_: 'Analyseren...',
download: 'Downloaden', download: 'Downloaden',
downloading_: 'Downloaden...', downloading_: 'Downloaden...',
view: 'Bekijken', view: 'Bekijken',

@ -100,9 +100,7 @@ Rails.application.routes.draw do
resource :debug, only: %i[show], controller: 'templates_debug' if Rails.env.development? resource :debug, only: %i[show], controller: 'templates_debug' if Rails.env.development?
resources :documents, only: %i[index create], controller: 'template_documents' resources :documents, only: %i[index create], controller: 'template_documents'
resources :clone_and_replace, only: %i[create], controller: 'templates_clone_and_replace' resources :clone_and_replace, only: %i[create], controller: 'templates_clone_and_replace'
if !Docuseal.multitenant? || Docuseal.demo? resources :detect_fields, only: %i[create], controller: 'templates_detect_fields' unless Docuseal.multitenant?
resources :detect_fields, only: %i[create], controller: 'templates_detect_fields'
end
resources :restore, only: %i[create], controller: 'templates_restore' resources :restore, only: %i[create], controller: 'templates_restore'
resources :archived, only: %i[index], controller: 'templates_archived_submissions' resources :archived, only: %i[index], controller: 'templates_archived_submissions'
resources :submissions, only: %i[new create] resources :submissions, only: %i[new create]

@ -39,6 +39,7 @@ Puma::Plugin.create do
configs = Sidekiq.configure_embed do |config| configs = Sidekiq.configure_embed do |config|
config.logger.level = Logger::INFO config.logger.level = Logger::INFO
sidekiq_config = YAML.load_file('config/sidekiq.yml') sidekiq_config = YAML.load_file('config/sidekiq.yml')
sidekiq_config['queues'] << 'fields' if ENV['DEMO'] == 'true'
config.queues = sidekiq_config['queues'] config.queues = sidekiq_config['queues']
config.concurrency = ENV.fetch('SIDEKIQ_THREADS', 5).to_i config.concurrency = ENV.fetch('SIDEKIQ_THREADS', 5).to_i
config.merge!(sidekiq_config) config.merge!(sidekiq_config)

@ -5,7 +5,7 @@ module Templates
module_function module_function
TextFieldBox = Struct.new(:x, :y, :w, :h, keyword_init: true) TextFieldBox = Struct.new(:x, :y, :w, :h, keyword_init: true)
PageNode = Struct.new(:prev, :next, :elem, :page, keyword_init: true) PageNode = Struct.new(:prev, :next, :elem, :page, :attachment_uuid, keyword_init: true)
DATE_REGEXP = / DATE_REGEXP = /
(?: (?:
@ -75,7 +75,7 @@ module Templates
{ {
uuid: SecureRandom.uuid, uuid: SecureRandom.uuid,
type: f.type, type: f.type,
required: true, required: f.type != 'checkbox',
preferences: {}, preferences: {},
areas: [{ areas: [{
x: f.x, x: f.x,
@ -90,14 +90,14 @@ module Templates
yield [attachment&.uuid, 0, fields] if block_given? yield [attachment&.uuid, 0, fields] if block_given?
fields [fields, nil]
end end
def process_pdf_attachment(io, attachment:, confidence:, nms:, temperature:, inference:, def process_pdf_attachment(io, attachment:, confidence:, nms:, temperature:, inference:,
split_page: false, aspect_ratio: false, padding: nil, regexp_type: false) split_page: false, aspect_ratio: false, padding: nil, regexp_type: false)
doc = Pdfium::Document.open_bytes(io.read) doc = Pdfium::Document.open_bytes(io.read)
head_node = PageNode.new(elem: ''.b) head_node = PageNode.new(elem: ''.b, page: 0, attachment_uuid: attachment&.uuid)
tail_node = head_node tail_node = head_node
fields = doc.page_count.times.flat_map do |page_number| fields = doc.page_count.times.flat_map do |page_number|
@ -107,7 +107,7 @@ module Templates
image = Vips::Image.new_from_memory(data, width, height, 4, :uchar) image = Vips::Image.new_from_memory(data, width, height, 4, :uchar)
fields = inference.call(image, confidence: confidence * 0.1, nms:, split_page:, fields = inference.call(image, confidence: confidence / 4.0, nms:, split_page:,
temperature:, aspect_ratio:, padding:) temperature:, aspect_ratio:, padding:)
text_fields = extract_text_fields_from_page(page) text_fields = extract_text_fields_from_page(page)
@ -118,15 +118,17 @@ module Templates
fields = fields.reject { |f| f.confidence < confidence } fields = fields.reject { |f| f.confidence < confidence }
field_nodes, tail_node = build_page_nodes(page, fields, tail_node) field_nodes, tail_node = build_page_nodes(page, fields, tail_node, attachment_uuid: attachment&.uuid)
fields = field_nodes.map do |node| fields = field_nodes.map do |node|
field = node.elem field = node.elem
type = regexp_type ? type_from_page_node(node) : field.type
{ {
uuid: SecureRandom.uuid, uuid: SecureRandom.uuid,
type: regexp_type ? type_from_page_node(node) : field.type, type:,
required: true, required: type != 'checkbox',
preferences: {}, preferences: {},
areas: [{ areas: [{
x: field.x, y: field.y, x: field.x, y: field.y,
@ -185,7 +187,7 @@ module Templates
return 'text' return 'text'
end end
def build_page_nodes(page, fields, tail_node) def build_page_nodes(page, fields, tail_node, attachment_uuid: nil)
field_nodes = [] field_nodes = []
current_text = ''.b current_text = ''.b
@ -243,13 +245,13 @@ module Templates
if process_field_node if process_field_node
unless current_text.empty? unless current_text.empty?
new_text_node = PageNode.new(prev: tail_node, elem: current_text, page: page.page_index) new_text_node = PageNode.new(prev: tail_node, elem: current_text, page: page.page_index, attachment_uuid:)
tail_node.next = new_text_node tail_node.next = new_text_node
tail_node = new_text_node tail_node = new_text_node
current_text = ''.b current_text = ''.b
end end
new_field_node = PageNode.new(prev: tail_node, elem: field, page: page.page_index) new_field_node = PageNode.new(prev: tail_node, elem: field, page: page.page_index, attachment_uuid:)
tail_node.next = new_field_node tail_node.next = new_field_node
tail_node = new_field_node tail_node = new_field_node
@ -299,7 +301,7 @@ module Templates
end end
unless current_text.empty? unless current_text.empty?
new_text_node = PageNode.new(prev: tail_node, elem: current_text, page: page.page_index) new_text_node = PageNode.new(prev: tail_node, elem: current_text, page: page.page_index, attachment_uuid:)
tail_node.next = new_text_node tail_node.next = new_text_node
tail_node = new_text_node tail_node = new_text_node
end end

Loading…
Cancel
Save