Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,73 +1,3 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
import random
|
5 |
-
|
6 |
-
# Download model from GitHub
|
7 |
-
model = torch.hub.load('ultralytics/yolov5', 'yolov5n')
|
8 |
-
|
9 |
-
# Initialize video capture
|
10 |
-
cap = cv2.VideoCapture('cars.mp4')
|
11 |
-
|
12 |
-
# Initialize text-to-speech engine
|
13 |
-
engine = pyttsx3.init()
|
14 |
-
|
15 |
-
# Simulated GPS location (latitude, longitude)
|
16 |
-
gps_location = (37.7749, -122.4194) # Example coordinates for San Francisco
|
17 |
-
|
18 |
-
# Function to speak the detected object
|
19 |
-
def speak(text):
|
20 |
-
engine.say(text)
|
21 |
-
engine.runAndWait()
|
22 |
-
|
23 |
-
while True:
|
24 |
-
ret, img = cap.read()
|
25 |
-
if not ret:
|
26 |
-
break
|
27 |
-
|
28 |
-
# Perform detection on the image
|
29 |
-
result = model(img)
|
30 |
-
print('result: ', result)
|
31 |
-
|
32 |
-
# Convert detected result to pandas DataFrame
|
33 |
-
data_frame = result.pandas().xyxy[0]
|
34 |
-
print('data_frame:')
|
35 |
-
print(data_frame)
|
36 |
-
|
37 |
-
# Get indexes of all the rows
|
38 |
-
indexes = data_frame.index
|
39 |
-
for index in indexes:
|
40 |
-
# Find the coordinate of top left corner of bounding box
|
41 |
-
x1 = int(data_frame['xmin'][index])
|
42 |
-
y1 = int(data_frame['ymin'][index])
|
43 |
-
# Find the coordinate of bottom right corner of bounding box
|
44 |
-
x2 = int(data_frame['xmax'][index])
|
45 |
-
y2 = int(data_frame['ymax'][index])
|
46 |
-
|
47 |
-
# Find label name and confidence score
|
48 |
-
label = data_frame['name'][index]
|
49 |
-
conf = data_frame['confidence'][index]
|
50 |
-
text = f"{label} {conf:.2f}"
|
51 |
-
|
52 |
-
# Draw bounding box and label on the image
|
53 |
-
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 255, 0), 2)
|
54 |
-
cv2.putText(img, text, (x1, y1 - 5), cv2.FONT_HERSHEY_PLAIN, 2, (255, 255, 0), 2)
|
55 |
-
|
56 |
-
# Context-aware actions based on detected objects
|
57 |
-
if label == "car" and conf > 0.5:
|
58 |
-
# Announce detected car and GPS location
|
59 |
-
speak(f"Car detected at GPS location: {gps_location[0]}, {gps_location[1]}")
|
60 |
-
# Here you can add more context-based features (e.g., alerting, saving data, etc.)
|
61 |
-
|
62 |
-
# Display GPS coordinates on the image
|
63 |
-
gps_text = f"GPS: {gps_location[0]:.4f}, {gps_location[1]:.4f}"
|
64 |
-
cv2.putText(img, gps_text, (10, 30), cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0), 2)
|
65 |
-
|
66 |
-
# Show the processed image
|
67 |
-
cv2.imshow('IMAGE', img)
|
68 |
-
if cv2.waitKey(1) & 0xFF == ord('q'):
|
69 |
-
break
|
70 |
-
|
71 |
-
# Release resources
|
72 |
-
cap.release()
|
73 |
-
cv2.destroyAllWindows()
|
|
|
1 |
+
git clone https://github.com/ultralytics/yolov5.git
|
2 |
+
cd yolov5
|
3 |
+
pip install -r requirements.txt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|