tahsinraiyan000 commited on
Commit
4544a40
·
verified ·
1 Parent(s): d50703f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -38
app.py CHANGED
@@ -4,7 +4,7 @@ import numpy as np
4
  from collections import defaultdict
5
  from transformers import pipeline
6
 
7
- # Initialize the model
8
  detector = pipeline("object-detection", model="facebook/detr-resnet-101")
9
 
10
  # Global counter
@@ -12,63 +12,34 @@ object_counter = defaultdict(int)
12
 
13
  def process_video(video_path):
14
  cap = cv2.VideoCapture(video_path)
15
-
16
  while cap.isOpened():
17
  ret, frame = cap.read()
18
  if not ret:
19
  break
20
 
21
- # Convert frame to RGB
22
  rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
23
-
24
- # Detect objects
25
  results = detector(rgb_frame, threshold=0.7)
26
 
27
- # Draw boxes and update counter
28
  for obj in results:
29
  label = obj["label"]
30
- score = obj["score"]
31
- box = obj["box"]
32
-
33
  object_counter[label] += 1
34
-
35
  xmin, ymin, xmax, ymax = int(box["xmin"]), int(box["ymin"]), int(box["xmax"]), int(box["ymax"])
36
  cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)
37
- cv2.putText(frame, f"{label} ({score:.2f})", (xmin, ymin-10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
38
 
39
- # Display counter
40
  counter_text = "\n".join([f"{k}: {v}" for k, v in object_counter.items()])
41
  cv2.putText(frame, counter_text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
42
-
43
  yield cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
44
 
45
  cap.release()
46
 
47
- # Gradio UI with Reset Button
48
  with gr.Blocks() as demo:
49
- gr.Markdown("# 🎥 Video Object Detection (DETR-R101)")
50
-
51
- with gr.Row():
52
- video_input = gr.Video(label="Upload Video")
53
- video_output = gr.Image(label="Detections")
54
-
55
- with gr.Row():
56
- # Add the reset button here
57
- reset_button = gr.Button("Reset Counter", variant="primary")
58
-
59
- # Process video when uploaded
60
- video_input.change(
61
- fn=process_video,
62
- inputs=video_input,
63
- outputs=video_output
64
- )
65
-
66
- # Reset counter when button clicked
67
- reset_button.click(
68
- fn=lambda: object_counter.clear(),
69
- inputs=None,
70
- outputs=None,
71
- queue=False # No need to wait in queue
72
- )
73
 
74
  demo.launch()
 
4
  from collections import defaultdict
5
  from transformers import pipeline
6
 
7
+ # Initialize the model (now works with timm installed)
8
  detector = pipeline("object-detection", model="facebook/detr-resnet-101")
9
 
10
  # Global counter
 
12
 
13
  def process_video(video_path):
14
  cap = cv2.VideoCapture(video_path)
 
15
  while cap.isOpened():
16
  ret, frame = cap.read()
17
  if not ret:
18
  break
19
 
 
20
  rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
 
 
21
  results = detector(rgb_frame, threshold=0.7)
22
 
 
23
  for obj in results:
24
  label = obj["label"]
 
 
 
25
  object_counter[label] += 1
26
+ box = obj["box"]
27
  xmin, ymin, xmax, ymax = int(box["xmin"]), int(box["ymin"]), int(box["xmax"]), int(box["ymax"])
28
  cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)
29
+ cv2.putText(frame, f"{label}", (xmin, ymin-10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
30
 
 
31
  counter_text = "\n".join([f"{k}: {v}" for k, v in object_counter.items()])
32
  cv2.putText(frame, counter_text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
 
33
  yield cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
34
 
35
  cap.release()
36
 
 
37
  with gr.Blocks() as demo:
38
+ gr.Markdown("# 🎥 Video Object Detection")
39
+ video_input = gr.Video(label="Upload Video")
40
+ video_output = gr.Image(label="Detections")
41
+ reset_button = gr.Button("Reset Counter")
42
+ video_input.change(process_video, video_input, video_output)
43
+ reset_button.click(lambda: object_counter.clear())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  demo.launch()