-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcardetection.py
81 lines (65 loc) · 2.6 KB
/
cardetection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import cv2
import pygame
from inference_sdk import InferenceHTTPClient
# Initialize Pygame
pygame.init()
# Define audio file paths for each language
audio_files = {
"english": "cardetected.mp3",
"hindi": "saamnegaadihai.mp3",
"kannada":"mundegaadiede.mp3"
# Add more languages as needed
}
# Create a dictionary to map language names to their respective audio file paths
language_to_audio_path = {
"english": audio_files["english"],
"hindi": audio_files["hindi"],
"kannada":audio_files["kannada"],
# Add more languages here
}
# Create a function to play the notification sound based on the selected language
def play_notification_sound(language):
if language in language_to_audio_path:
audio_file_path = language_to_audio_path[language]
notification_sound = pygame.mixer.Sound(audio_file_path)
notification_sound.play()
else:
print("Language not supported")
# Create an inference client
CLIENT = InferenceHTTPClient(
api_url="https://detect.roboflow.com",
api_key="PIT9ordmgBwkNFwOiLft")
# Capture video from the camera
cap = cv2.VideoCapture(0) # Change the argument to the appropriate camera index if you have multiple cameras
# Allow the user to select their preferred language
selected_language = input("Select your preferred language (e.g., 'english', 'hindi','kannada'): ").lower()
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# Run inference on the frame
result = CLIENT.infer(frame, model_id="car_detection-3zagb/1")
# Extract and print object names
if 'predictions' in result:
object_names = [prediction['class'] for prediction in result['predictions']]
# Check if "pothole" is detected
if '1' in object_names:
# Play notification sound based on the selected language
play_notification_sound(selected_language)
# Draw bounding boxes
for bounding_box in result['predictions']:
x1 = bounding_box['x'] - bounding_box['width'] / 2
x2 = bounding_box['x'] + bounding_box['width'] / 2
y1 = bounding_box['y'] - bounding_box['height'] / 2
y2 = bounding_box['y'] + bounding_box['height'] / 2
box = (int(x1), int(y1)), (int(x2), int(y2))
cv2.rectangle(frame, box[0], box[1], (0, 255, 0), 2)
# Display the resulting frame
cv2.imshow('frame', frame)
# Exit if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the camera and close OpenCV windows
cap.release()
cv2.destroyAllWindows()
# Quit Pygame
pygame.quit()