forked from TakshDhabalia/Driving_Optimization
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnon_threading.py
More file actions
79 lines (65 loc) · 2.68 KB
/
non_threading.py
File metadata and controls
79 lines (65 loc) · 2.68 KB
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
import os
import time
import subprocess
import MQTT
import osp
import Camera as cam
# Global variable to store car detection results
car_count = 0
# Function to run YOLOv5 detection on an image
def run_yolov5_detection(image_path):
global car_count
# Run YOLOv5 detection
command = [
"python", "detect.py",
"--weights", "yolov5s.pt",
"--source", image_path,
"--conf-thres", "0.25", # Confidence threshold for detection
"--iou-thres", "0.45", # IoU threshold for NMS
"--save-txt", # Save results to text files
"--classes", "2", # Specify only car class
"--project", "runs/detect", # Output directory
"--name", "exp1", # Experiment name
"--exist-ok" # Overwrite if exists
]
subprocess.run(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
# Function to count cars in a label file
def count_cars_in_label_file(label_file_path):
car_count = 0
with open(label_file_path, 'r') as file:
for line in file:
class_id = line.split()[0]
if class_id == '2': # Check if the class ID is for a car
car_count += 1
return car_count
# Function to iterate through label files and count cars
def count_cars_in_label_files(labels_directory):
total_car_count = 0
for file_name in os.listdir(labels_directory):
if file_name.endswith('.txt'):
label_file_path = os.path.join(labels_directory, file_name)
car_count_in_file = count_cars_in_label_file(label_file_path)
total_car_count += car_count_in_file
return total_car_count
# Function to continuously monitor images and perform detection
def monitor_images(freq):
global car_count
for i in range(freq):
# Capture image
cam.capture_images("http://192.168.1.14:8080/video", 3)
# Run YOLOv5 detection on the captured image
run_yolov5_detection(f"images/{i}.jpeg")
# Count cars in label files
current_car_count = count_cars_in_label_files('runs/detect/exp1/labels')
# Print car count information
print(f"At t={i+1}, Total Cars Detected: {current_car_count} in {i}.jpeg")
time.sleep(2)
if __name__ == "__main__":
# Create the images directory if not exists
if not os.path.exists("images"):
os.makedirs("images")
# Start monitoring images sequentially
client = MQTT.MQTTClient("172.28.66.244", 2000)
client.connect()
monitor_images(100)
client.publish_loop("ESIOT", monitor_images(freq=100), 100)