nooneshouldtouch commited on
Commit
a5a013d
·
verified ·
1 Parent(s): 6411b9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -41
app.py CHANGED
@@ -67,10 +67,9 @@ class_names = ['H', 'V', 'W']
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,17 +83,10 @@ def prepare_model(approach: int):
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():
@@ -125,35 +117,12 @@ async def upload_file(approach: int = Form(...), file: UploadFile = File(...)):
125
  db.refresh(upload_obj)
126
  pdf_path = generate_pdf(upload_obj)
127
  db.close()
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
141
-
142
- def generate_pdf(upload_obj: Upload):
143
- buffer = BytesIO()
144
- doc = SimpleDocTemplate(buffer, pagesize=A4)
145
- elements = []
146
- styles = getSampleStyleSheet()
147
- elements.append(Paragraph("Industrial Safety Report", styles["Title"]))
148
- elements.append(Paragraph(f"Filename: {upload_obj.filename}", styles["Normal"]))
149
- elements.append(Paragraph(f"Timestamp: {upload_obj.timestamp.strftime('%Y-%m-%d %H:%M:%S')}", styles["Normal"]))
150
- data = [["Total Workers", upload_obj.total_workers], ["Total Helmets", upload_obj.total_helmets], ["Total Vests", upload_obj.total_vests]]
151
- table = Table(data)
152
- table.setStyle(TableStyle([("BACKGROUND", (0, 0), (-1, 0), colors.grey), ("TEXTCOLOR", (0, 0), (-1, 0), colors.whitesmoke), ("GRID", (0, 0), (-1, -1), 1, colors.black)]))
153
- elements.append(table)
154
- doc.build(elements)
155
- buffer.seek(0)
156
- pdf_path = os.path.join(PROCESSED_FOLDER, f"report_{upload_obj.id}.pdf")
157
- with open(pdf_path, "wb") as f:
158
- f.write(buffer.getvalue())
159
- return pdf_path
 
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
  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():
 
117
  db.refresh(upload_obj)
118
  pdf_path = generate_pdf(upload_obj)
119
  db.close()
120
+ return {
121
+ "message": "File processed successfully.",
122
+ "upload_id": upload_id,
123
+ "total_workers": upload_obj.total_workers,
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
+ }