Spaces:
Runtime error
Runtime error
Annas Dev
commited on
Commit
·
ffd0bdc
1
Parent(s):
8a59887
return json
Browse files- app.py +13 -7
- inference/annotate_image.py +5 -1
- inference/inference_handler.py +6 -1
- inference/ocr.py +1 -1
app.py
CHANGED
@@ -27,7 +27,7 @@ def run_inference(model_path, images_path):
|
|
27 |
inference_batch = prepare_batch_for_inference(images_path)
|
28 |
context = {"model_dir": model_path}
|
29 |
print('handle....')
|
30 |
-
handle(inference_batch,context)
|
31 |
except Exception as err:
|
32 |
print('err...', err)
|
33 |
|
@@ -44,15 +44,21 @@ def run(img_path):
|
|
44 |
print('img path: ', img_path)
|
45 |
model_path = get_model()
|
46 |
model_path = pretrained_model(model_path)
|
47 |
-
run_inference(model_path, [img_path])
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
|
|
|
54 |
|
55 |
|
56 |
gr.Markdown('Upload Foto Wajah Kamu (Pastikan hanya terdapat SATU wajah pada)')
|
57 |
-
iface = gr.Interface(fn=run, inputs=gr.Image(type="filepath"), outputs="image")
|
58 |
iface.launch()
|
|
|
27 |
inference_batch = prepare_batch_for_inference(images_path)
|
28 |
context = {"model_dir": model_path}
|
29 |
print('handle....')
|
30 |
+
return handle(inference_batch,context)
|
31 |
except Exception as err:
|
32 |
print('err...', err)
|
33 |
|
|
|
44 |
print('img path: ', img_path)
|
45 |
model_path = get_model()
|
46 |
model_path = pretrained_model(model_path)
|
47 |
+
results = run_inference(model_path, [img_path])
|
48 |
|
49 |
+
if len(results) == 0:
|
50 |
+
return Image.open(img_path), {'status': 'error'}
|
51 |
+
|
52 |
+
# img_name = os.path.basename(img_path)
|
53 |
+
# ext = os.path.splitext(img_name)[1]
|
54 |
+
# img_name = img_name[:img_name.find('.')]
|
55 |
+
|
56 |
+
# return Image.open(os.path.join('tmp', f'{img_name}_inference.jpg'))
|
57 |
|
58 |
+
print(results[0])
|
59 |
+
return Image.open(results[0]['image_path']), results[0]
|
60 |
|
61 |
|
62 |
gr.Markdown('Upload Foto Wajah Kamu (Pastikan hanya terdapat SATU wajah pada)')
|
63 |
+
iface = gr.Interface(fn=run, inputs=gr.Image(type="filepath"), outputs=["image","json"])
|
64 |
iface.launch()
|
inference/annotate_image.py
CHANGED
@@ -34,8 +34,10 @@ def annotate_image(image_path, annotation_object):
|
|
34 |
draw = ImageDraw.Draw(overlay)
|
35 |
font = ImageFont.load_default()
|
36 |
|
|
|
37 |
predictions = [span['label'] for span in annotation_object['output']]
|
38 |
boxes = [span['words'][0]['box'] for span in annotation_object['output']]
|
|
|
39 |
for prediction, box in zip(predictions, boxes):
|
40 |
draw.rectangle(box, outline=label2color[prediction],
|
41 |
width=3, fill=label2color[prediction]+(int(255*0.33),))
|
@@ -47,4 +49,6 @@ def annotate_image(image_path, annotation_object):
|
|
47 |
|
48 |
image_name = os.path.basename(image_path)
|
49 |
image_name = image_name[:image_name.find('.')]
|
50 |
-
|
|
|
|
|
|
34 |
draw = ImageDraw.Draw(overlay)
|
35 |
font = ImageFont.load_default()
|
36 |
|
37 |
+
print('annotation_object ---> ',annotation_object)
|
38 |
predictions = [span['label'] for span in annotation_object['output']]
|
39 |
boxes = [span['words'][0]['box'] for span in annotation_object['output']]
|
40 |
+
receipt_info = [{span['label']: span['text']} for span in annotation_object['output']]
|
41 |
for prediction, box in zip(predictions, boxes):
|
42 |
draw.rectangle(box, outline=label2color[prediction],
|
43 |
width=3, fill=label2color[prediction]+(int(255*0.33),))
|
|
|
49 |
|
50 |
image_name = os.path.basename(image_path)
|
51 |
image_name = image_name[:image_name.find('.')]
|
52 |
+
image_inf_path = f'tmp/{image_name}_inference.jpg'
|
53 |
+
img.save(image_inf_path)
|
54 |
+
return {'image_path': image_inf_path, 'data': receipt_info}
|
inference/inference_handler.py
CHANGED
@@ -178,8 +178,13 @@ class ModelHandler(object):
|
|
178 |
inf_out.write(inference_out)
|
179 |
inference_out_list = json.loads(inference_out)
|
180 |
flattened_output_list = get_flattened_output(inference_out_list)
|
|
|
|
|
181 |
for i, flattened_output in enumerate(flattened_output_list):
|
182 |
-
annotate_image(data['image_path'][i], flattened_output)
|
|
|
|
|
|
|
183 |
|
184 |
|
185 |
|
|
|
178 |
inf_out.write(inference_out)
|
179 |
inference_out_list = json.loads(inference_out)
|
180 |
flattened_output_list = get_flattened_output(inference_out_list)
|
181 |
+
|
182 |
+
result = []
|
183 |
for i, flattened_output in enumerate(flattened_output_list):
|
184 |
+
res = annotate_image(data['image_path'][i], flattened_output)
|
185 |
+
result.append(res)
|
186 |
+
|
187 |
+
return result
|
188 |
|
189 |
|
190 |
|
inference/ocr.py
CHANGED
@@ -56,5 +56,5 @@ def prepare_batch_for_inference(image_paths):
|
|
56 |
"bboxes": boxes_lists,
|
57 |
"words": word_lists
|
58 |
}
|
59 |
-
|
60 |
return inference_batch
|
|
|
56 |
"bboxes": boxes_lists,
|
57 |
"words": word_lists
|
58 |
}
|
59 |
+
# print('inference_batch:', inference_batch)
|
60 |
return inference_batch
|