Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
@@ -1,99 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import numpy as np
|
3 |
-
import pandas as pd
|
4 |
-
from app_utils import annotate_planogram_compliance, do_sorting, xml_to_csv
|
5 |
-
from inference import run
|
6 |
-
import json
|
7 |
-
import os
|
8 |
-
from tempfile import NamedTemporaryFile
|
9 |
-
|
10 |
-
|
11 |
-
# Target names list
|
12 |
-
target_names = [
|
13 |
-
"Bottle,100PLUS ACTIVE 1.5L",
|
14 |
-
"Bottle,100PLUS ACTIVE 500ML",
|
15 |
-
"Bottle,100PLUS LEMON LIME 1.5L",
|
16 |
-
# Add all other target names here
|
17 |
-
]
|
18 |
-
|
19 |
-
# Define the function to run planogram compliance check
|
20 |
-
def planogram_compliance_check(planogram_image, master_planogram_image, annotation_file):
|
21 |
-
# Convert uploaded images to numpy arrays
|
22 |
-
planogram_img = np.array(planogram_image)
|
23 |
-
master_planogram_img = np.array(master_planogram_image)
|
24 |
-
|
25 |
-
# Perform inference on planogram image
|
26 |
-
result_list = run(
|
27 |
-
weights="base_line_best_model_exp5.pt",
|
28 |
-
source=planogram_img,
|
29 |
-
imgsz=[640, 640],
|
30 |
-
conf_thres=0.6,
|
31 |
-
iou_thres=0.6,
|
32 |
-
)
|
33 |
-
|
34 |
-
# Load annotation file and convert to DataFrame
|
35 |
-
if annotation_file is not None:
|
36 |
-
annotation_df = xml_to_csv(annotation_file)
|
37 |
-
sorted_xml_df = do_sorting(annotation_df)
|
38 |
-
else:
|
39 |
-
sorted_xml_df = None
|
40 |
-
|
41 |
-
# Run planogram compliance check
|
42 |
-
compliance_score, annotated_image = run_compliance_check(
|
43 |
-
planogram_img, master_planogram_img, sorted_xml_df, result_list
|
44 |
-
)
|
45 |
-
|
46 |
-
return compliance_score, annotated_image
|
47 |
-
|
48 |
-
def run_compliance_check(planogram_img, master_planogram_img, sorted_xml_df, result_list):
|
49 |
-
# Placeholder for actual score calculation
|
50 |
-
compliance_score = 0.0
|
51 |
-
|
52 |
-
# Placeholder for annotated image
|
53 |
-
annotated_image = planogram_img.copy()
|
54 |
-
|
55 |
-
if sorted_xml_df is not None:
|
56 |
-
annotate_df = sorted_xml_df[["xmin", "ymin", "xmax", "ymax", "line_number", "cls"]].astype(int)
|
57 |
-
else:
|
58 |
-
annotate_df = None
|
59 |
-
|
60 |
-
mask = master_table != non_null_product
|
61 |
-
m_detected_table = np.ma.masked_array(master_table, mask=mask)
|
62 |
-
m_annotated_table = np.ma.masked_array(detected_table, mask=mask)
|
63 |
-
|
64 |
-
# wrong_indexes = np.ravel_multi_index(master_table*mask != detected_table*mask, master_table.shape)
|
65 |
-
wrong_indexes = np.where(master_table != detected_table)
|
66 |
-
correct_indexes = np.where(master_table == detected_table)
|
67 |
-
|
68 |
-
# Annotate planogram compliance on the image
|
69 |
-
annotated_image = annotate_planogram_compliance(
|
70 |
-
annotated_image, annotate_df, correct_indexes, wrong_indexes, target_names
|
71 |
-
)
|
72 |
-
|
73 |
-
# Calculate compliance score
|
74 |
-
correct_matches = (np.ma.masked_equal(master_table, non_null_product) == detected_table).sum()
|
75 |
-
total_products = (master_table != non_null_product).sum()
|
76 |
-
if total_products != 0:
|
77 |
-
compliance_score = correct_matches / total_products
|
78 |
-
|
79 |
-
return compliance_score, annotated_image
|
80 |
-
|
81 |
-
|
82 |
-
# Gradio interface
|
83 |
-
planogram_check_interface = gr.Interface(
|
84 |
-
fn=planogram_compliance_check,
|
85 |
-
inputs=[
|
86 |
-
gr.inputs.Image(label="Planogram Image"),
|
87 |
-
gr.inputs.Image(label="Master Planogram Image"),
|
88 |
-
gr.inputs.Dataframe(label="Annotation File (XML)")
|
89 |
-
],
|
90 |
-
outputs=[
|
91 |
-
gr.outputs.Textbox(label="Compliance Score"),
|
92 |
-
gr.outputs.Image(label="Annotated Planogram Image"),
|
93 |
-
],
|
94 |
-
title="Planogram Compliance Checker",
|
95 |
-
description="Upload planogram image, master planogram image, and annotation file (if available) to check compliance."
|
96 |
-
)
|
97 |
-
|
98 |
-
|
99 |
-
planogram_check_interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|