techysanoj commited on
Commit
d10f466
·
1 Parent(s): 95b06b4

creating app.py file

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import torch
4
+ from torchvision import transforms
5
+ from PIL import Image
6
+
7
+ # Load the pre-trained object detection model (replace with your own model)
8
+ # For example, using a torchvision model for demonstration purposes
9
+ model = torch.hub.load('pytorch/vision:v0.10.0', 'fasterrcnn_resnet50_fpn', pretrained=True)
10
+ model.eval()
11
+
12
+ # Define the transformations for the input image
13
+ transform = transforms.Compose([
14
+ transforms.ToTensor(),
15
+ ])
16
+
17
+ # Function to perform object detection on an image
18
+ def detect_objects(image):
19
+ # Convert image to tensor
20
+ input_tensor = transform(image).unsqueeze(0)
21
+
22
+ # Perform object detection
23
+ with torch.no_grad():
24
+ predictions = model(input_tensor)
25
+
26
+ # Extract bounding boxes and labels from predictions
27
+ boxes = predictions[0]['boxes'].numpy()
28
+ labels = predictions[0]['labels'].numpy()
29
+
30
+ return boxes, labels
31
+
32
+ # Function for live object detection from the camera
33
+ def live_object_detection():
34
+ # Open a connection to the camera (replace with your own camera setup)
35
+ cap = cv2.VideoCapture(0)
36
+
37
+ while True:
38
+ # Capture frame-by-frame
39
+ ret, frame = cap.read()
40
+
41
+ # Convert the frame to PIL Image
42
+ frame_pil = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
43
+
44
+ # Perform object detection
45
+ boxes, labels = detect_objects(frame_pil)
46
+
47
+ # Draw bounding boxes on the frame
48
+ for box, label in zip(boxes, labels):
49
+ box = [int(coord) for coord in box]
50
+ cv2.rectangle(frame, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 2)
51
+ cv2.putText(frame, f"Label: {label}", (box[0], box[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
52
+
53
+ # Display the resulting frame
54
+ cv2.imshow('Object Detection', frame)
55
+
56
+ # Break the loop when 'q' key is pressed
57
+ if cv2.waitKey(1) & 0xFF == ord('q'):
58
+ break
59
+
60
+ # Release the camera and close all windows
61
+ cap.release()
62
+ cv2.destroyAllWindows()
63
+
64
+ # Define the Gradio interface
65
+ iface = gr.Interface(
66
+ fn=[detect_objects, live_object_detection],
67
+ inputs=[
68
+ gr.Image(type="pil", label="Upload a photo for object detection"),
69
+ "webcam",
70
+ ],
71
+ outputs="image",
72
+ live=True,
73
+ )
74
+
75
+ # Launch the Gradio interface
76
+ iface.launch()