Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -67,9 +67,10 @@ class_names = ['H', 'V', 'W']
|
|
67 |
num_classes = len(class_names)
|
68 |
num_anchors = 9
|
69 |
model = None
|
|
|
70 |
|
71 |
def prepare_model(approach: int):
|
72 |
-
global model
|
73 |
if approach not in [1, 2, 3]:
|
74 |
raise ValueError("Approach must be 1, 2, or 3.")
|
75 |
|
@@ -83,10 +84,17 @@ def prepare_model(approach: int):
|
|
83 |
if not os.path.exists(weight_path):
|
84 |
raise FileNotFoundError(f"Weight file not found: {weight_path}")
|
85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
input_tensor = Input(shape=(input_shape[0], input_shape[1], 3))
|
87 |
num_out_filters = (num_anchors // 3) * (5 + num_classes)
|
88 |
model = yolo_body(input_tensor, num_out_filters)
|
89 |
model.load_weights(weight_path)
|
|
|
90 |
|
91 |
@app.on_event("startup")
|
92 |
def on_startup():
|
@@ -120,13 +128,13 @@ async def upload_file(approach: int = Form(...), file: UploadFile = File(...)):
|
|
120 |
return {"message": "File processed successfully.", "upload_id": upload_id, "pdf_path": pdf_path}
|
121 |
|
122 |
def run_detection_on_frame(frame: np.ndarray, upload_id: int, db: Session) -> np.ndarray:
|
123 |
-
global model
|
124 |
ih, iw = frame.shape[:2]
|
125 |
resized = letterbox_image(frame, input_shape)
|
126 |
resized_expanded = np.expand_dims(resized, 0)
|
127 |
image_data = np.array(resized_expanded) / 255.0
|
128 |
prediction = model.predict(image_data)
|
129 |
-
boxes = detection(prediction,
|
130 |
for box in boxes:
|
131 |
x1, y1, x2, y2, _, cls_id = map(int, box)
|
132 |
return frame
|
|
|
67 |
num_classes = len(class_names)
|
68 |
num_anchors = 9
|
69 |
model = None
|
70 |
+
anchor_boxes = None
|
71 |
|
72 |
def prepare_model(approach: int):
|
73 |
+
global model, anchor_boxes
|
74 |
if approach not in [1, 2, 3]:
|
75 |
raise ValueError("Approach must be 1, 2, or 3.")
|
76 |
|
|
|
84 |
if not os.path.exists(weight_path):
|
85 |
raise FileNotFoundError(f"Weight file not found: {weight_path}")
|
86 |
|
87 |
+
anchor_boxes = np.array([
|
88 |
+
np.array([[10, 13], [16, 30], [33, 23]]) / 32,
|
89 |
+
np.array([[30, 61], [62, 45], [59, 119]]) / 16,
|
90 |
+
np.array([[116, 90], [156, 198], [373, 326]]) / 8
|
91 |
+
], dtype="float64")
|
92 |
+
|
93 |
input_tensor = Input(shape=(input_shape[0], input_shape[1], 3))
|
94 |
num_out_filters = (num_anchors // 3) * (5 + num_classes)
|
95 |
model = yolo_body(input_tensor, num_out_filters)
|
96 |
model.load_weights(weight_path)
|
97 |
+
print(f"YOLO model (Approach={approach}) loaded successfully.")
|
98 |
|
99 |
@app.on_event("startup")
|
100 |
def on_startup():
|
|
|
128 |
return {"message": "File processed successfully.", "upload_id": upload_id, "pdf_path": pdf_path}
|
129 |
|
130 |
def run_detection_on_frame(frame: np.ndarray, upload_id: int, db: Session) -> np.ndarray:
|
131 |
+
global model, anchor_boxes
|
132 |
ih, iw = frame.shape[:2]
|
133 |
resized = letterbox_image(frame, input_shape)
|
134 |
resized_expanded = np.expand_dims(resized, 0)
|
135 |
image_data = np.array(resized_expanded) / 255.0
|
136 |
prediction = model.predict(image_data)
|
137 |
+
boxes = detection(prediction, anchor_boxes, len(class_names), (ih, iw), input_shape, 50, 0.3, 0.45, False)[0].numpy()
|
138 |
for box in boxes:
|
139 |
x1, y1, x2, y2, _, cls_id = map(int, box)
|
140 |
return frame
|