nooneshouldtouch commited on
Commit
3f56989
·
verified ·
1 Parent(s): a5a013d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -2
app.py CHANGED
@@ -93,6 +93,35 @@ def on_startup():
93
  fix_tf_gpu()
94
  prepare_model(approach=1)
95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  @app.post("/upload")
97
  async def upload_file(approach: int = Form(...), file: UploadFile = File(...)):
98
  global model
@@ -115,7 +144,6 @@ async def upload_file(approach: int = Form(...), file: UploadFile = File(...)):
115
  processed_path = os.path.join(PROCESSED_FOLDER, processed_filename)
116
  cv2.imwrite(processed_path, processed_img)
117
  db.refresh(upload_obj)
118
- pdf_path = generate_pdf(upload_obj)
119
  db.close()
120
  return {
121
  "message": "File processed successfully.",
@@ -124,5 +152,5 @@ async def upload_file(approach: int = Form(...), file: UploadFile = File(...)):
124
  "total_helmets": upload_obj.total_helmets,
125
  "total_vests": upload_obj.total_vests,
126
  "detected_items": upload_obj.worker_images.split(',') if upload_obj.worker_images else [],
127
- "pdf_download_link": pdf_path
128
  }
 
93
  fix_tf_gpu()
94
  prepare_model(approach=1)
95
 
96
+ def run_detection_on_frame(frame: np.ndarray, upload_id: int, db: Session) -> np.ndarray:
97
+ global model, input_shape, class_names
98
+ ih, iw = frame.shape[:2]
99
+ resized = letterbox_image(frame, input_shape)
100
+ image_data = np.expand_dims(resized, 0) / 255.0
101
+ prediction = model.predict(image_data)
102
+ boxes = detection(
103
+ prediction, None, len(class_names), (ih, iw), input_shape, 50, 0.3, 0.45, False
104
+ )[0].numpy()
105
+ workers, helmets, vests = [], [], []
106
+ for box in boxes:
107
+ x1, y1, x2, y2, score, cls_id = box
108
+ x1, y1, x2, y2 = map(int, [x1, y1, x2, y2])
109
+ cls_id = int(cls_id)
110
+ label = class_names[cls_id]
111
+ color = (0, 255, 0) if label == 'W' else (255, 0, 0) if label == 'H' else (0, 0, 255)
112
+ cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
113
+ cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2)
114
+ if label == 'W': workers.append((x1, y1, x2, y2))
115
+ elif label == 'H': helmets.append((x1, y1, x2, y2))
116
+ elif label == 'V': vests.append((x1, y1, x2, y2))
117
+ upload_obj = db.query(Upload).filter(Upload.id == upload_id).first()
118
+ if upload_obj:
119
+ upload_obj.total_workers += len(workers)
120
+ upload_obj.total_helmets += len(helmets)
121
+ upload_obj.total_vests += len(vests)
122
+ db.commit()
123
+ return frame
124
+
125
  @app.post("/upload")
126
  async def upload_file(approach: int = Form(...), file: UploadFile = File(...)):
127
  global model
 
144
  processed_path = os.path.join(PROCESSED_FOLDER, processed_filename)
145
  cv2.imwrite(processed_path, processed_img)
146
  db.refresh(upload_obj)
 
147
  db.close()
148
  return {
149
  "message": "File processed successfully.",
 
152
  "total_helmets": upload_obj.total_helmets,
153
  "total_vests": upload_obj.total_vests,
154
  "detected_items": upload_obj.worker_images.split(',') if upload_obj.worker_images else [],
155
+ "processed_image": processed_path
156
  }