SakibRumu commited on
Commit
e51c033
·
verified ·
1 Parent(s): be3f638

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -5
app.py CHANGED
@@ -17,20 +17,30 @@ model = YOLO("/home/user/app/best.pt") # আপনি যেই path এ best.p
17
 
18
  # Frame processing function
19
  def process_frame(frame):
20
- img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
 
 
 
21
  img_tensor = torch.from_numpy(img).permute(2, 0, 1).float() / 255.0
22
  img_tensor = img_tensor.unsqueeze(0)
23
 
 
24
  results = model(img_tensor, augment=False)
25
- results = model.nms(results)[0]
 
 
26
 
27
  extracted_texts = []
28
  confidences = []
29
 
30
- for det in results:
31
- x1, y1, x2, y2, conf, cls = det.tolist()
32
  if conf > 0.5:
33
- x1, y1, x2, y2 = map(int, (x1, y1, x2, y2))
 
 
 
 
34
  cls = int(cls)
35
 
36
  label_map = {0: "Analog", 1: "Digital", 2: "Non-LP"}
@@ -51,6 +61,7 @@ def process_frame(frame):
51
 
52
  return frame, "\n".join(extracted_texts), ", ".join(confidences)
53
 
 
54
  # Input handler
55
  def process_input(input_file):
56
  file_path = input_file.name
 
17
 
18
  # Frame processing function
19
  def process_frame(frame):
20
+ # Resize image to 640x640
21
+ frame_resized = cv2.resize(frame, (640, 640))
22
+
23
+ img = cv2.cvtColor(frame_resized, cv2.COLOR_BGR2RGB)
24
  img_tensor = torch.from_numpy(img).permute(2, 0, 1).float() / 255.0
25
  img_tensor = img_tensor.unsqueeze(0)
26
 
27
+ # Run inference with the YOLO model (no need to manually apply nms)
28
  results = model(img_tensor, augment=False)
29
+
30
+ # Extract results (list of detections)
31
+ detections = results.xywh[0] # YOLO's detection results
32
 
33
  extracted_texts = []
34
  confidences = []
35
 
36
+ for det in detections:
37
+ x_center, y_center, width, height, conf, cls = det.tolist()
38
  if conf > 0.5:
39
+ # Convert from YOLO format to bounding box format
40
+ x1 = int((x_center - width / 2) * 640)
41
+ y1 = int((y_center - height / 2) * 640)
42
+ x2 = int((x_center + width / 2) * 640)
43
+ y2 = int((y_center + height / 2) * 640)
44
  cls = int(cls)
45
 
46
  label_map = {0: "Analog", 1: "Digital", 2: "Non-LP"}
 
61
 
62
  return frame, "\n".join(extracted_texts), ", ".join(confidences)
63
 
64
+
65
  # Input handler
66
  def process_input(input_file):
67
  file_path = input_file.name