techysanoj commited on
Commit
4cbee43
·
1 Parent(s): 69c84d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -48
app.py CHANGED
@@ -1,8 +1,8 @@
1
  import gradio as gr
2
- import cv2
3
  import torch
4
  from PIL import Image
5
  from transformers import DetrImageProcessor, DetrForObjectDetection
 
6
  import numpy as np
7
 
8
  # Load the pre-trained DETR model
@@ -33,55 +33,11 @@ def image_object_detection(image_pil):
33
 
34
  return image_np
35
 
36
- # Function for live object detection from the camera
37
- def live_object_detection():
38
- # Open a connection to the camera (replace with your own camera setup)
39
- cap = cv2.VideoCapture(0)
40
-
41
- while True:
42
- # Capture frame-by-frame
43
- ret, frame = cap.read()
44
-
45
- # Convert the frame to PIL Image
46
- frame_pil = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
47
-
48
- # Process the frame with the DETR model
49
- inputs = processor(images=frame_pil, return_tensors="pt")
50
- outputs = model(**inputs)
51
-
52
- # convert outputs (bounding boxes and class logits) to COCO API
53
- # let's only keep detections with score > 0.9
54
- target_sizes = torch.tensor([frame_pil.size[::-1]])
55
- results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
56
-
57
- # Draw bounding boxes on the frame
58
- for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
59
- box = [int(round(i)) for i in box.tolist()]
60
- cv2.rectangle(frame, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 2)
61
- label_text = f"{model.config.id2label[label.item()]}: {round(score.item(), 3)}"
62
- cv2.putText(frame, label_text, (box[0], box[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
63
-
64
- # Display the resulting frame
65
- cv2.imshow('Object Detection', frame)
66
-
67
- # Break the loop when 'q' key is pressed
68
- if cv2.waitKey(1) & 0xFF == ord('q'):
69
- break
70
-
71
- # Release the camera and close all windows
72
- cap.release()
73
- cv2.destroyAllWindows()
74
-
75
  # Define the Gradio interface
76
  iface = gr.Interface(
77
- fn=[image_object_detection, live_object_detection],
78
- inputs=[
79
- gr.Image(type="pil", label="Upload an image for object detection") # Remove this line
80
- ],
81
- outputs=[
82
- "image",
83
- "image",
84
- ],
85
  live=True,
86
  )
87
 
 
1
  import gradio as gr
 
2
  import torch
3
  from PIL import Image
4
  from transformers import DetrImageProcessor, DetrForObjectDetection
5
+ import cv2
6
  import numpy as np
7
 
8
  # Load the pre-trained DETR model
 
33
 
34
  return image_np
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  # Define the Gradio interface
37
  iface = gr.Interface(
38
+ fn=image_object_detection,
39
+ inputs=gr.Image(type="pil", label="Upload an image for object detection"),
40
+ outputs="image",
 
 
 
 
 
41
  live=True,
42
  )
43