𝗜𝘁 𝗶𝘀 𝘀𝗼𝗺𝗲𝘁𝗶𝗺𝗲𝘀 𝗰𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗶𝗻𝗴 𝘁𝗼 𝗳𝗶𝗻𝗱 𝗮 𝗽𝗮𝗿𝗸𝗶𝗻𝗴 𝗹𝗼𝘁 𝗶𝗻 𝗳𝗿𝗼𝗻𝘁 𝗼𝗳 𝗺𝘆 𝗮𝗽𝗮𝗿𝘁𝗺𝗲𝗻𝘁 𝘀𝗶𝗻𝗰𝗲 𝗜 𝗱𝗼 𝗻𝗼𝘁 𝗵𝗮𝘃𝗲 𝗮 𝗽𝗿𝗶𝘃𝗮𝘁𝗲 𝗽𝗮𝗿𝗸𝗶𝗻𝗴 𝗹𝗼𝘁. 𝗧𝗵𝗲𝗿𝗲𝗳𝗼𝗿𝗲, 𝗜 𝗱𝗲𝗰𝗶𝗱𝗲𝗱 𝘁𝗼 𝗱𝗲𝘃𝗲𝗹𝗼𝗽 𝗮𝗻𝗱 𝘂𝘀𝗲 𝗮 𝘀𝘆𝘀𝘁𝗲𝗺 𝘁𝗼 𝗺𝗼𝗻𝗶𝘁𝗼𝗿 𝘁𝗵𝗲 𝗽𝗿𝗲𝘀𝗲𝗻𝗰𝗲 𝗼𝗳 𝘁𝗵𝗲 𝗰𝗮𝗿𝘀 𝗼𝗻 𝘁𝗵𝗲 𝘀𝘁𝗿𝗲𝗲𝘁 𝗮𝗻𝗱 𝘁𝗿𝗮𝗻𝘀𝗳𝗲𝗿 𝘁𝗵𝗲 𝗻𝘂𝗺𝗯𝗲𝗿 𝗼𝗳 𝗳𝗿𝗲𝗲 𝗽𝗮𝗿𝗸𝗶𝗻𝗴 lot 𝘃𝗶𝗮 𝗠𝗤𝗧𝗧 𝘁𝗼 𝗺𝘆 𝗠𝗼𝗯𝗶𝗹𝗲 𝗔𝗽𝗽. 𝗧𝗵𝗲 𝗔𝗽𝗽 𝗻𝗮𝗺𝗲 𝗶𝘀 𝗜𝗢𝗧 𝗠𝗤𝗧𝗧 𝗣𝗮𝗻𝗲𝗹, 𝘄𝗵𝗶𝗰𝗵 𝘆𝗼𝘂 𝗰𝗮𝗻 𝗳𝗶𝗻𝗱 𝗶𝗻 𝘁𝗵𝗲 𝗚𝗼𝗼𝗴𝗹𝗲 𝗽𝗹𝗮𝘆 𝗦𝘁𝗼𝗿𝗲, 𝗮𝗻𝗱 𝗶𝘁 𝘄𝗼𝗿𝗸𝘀 𝘄𝗲𝗹𝗹. 𝗜𝗻 𝗧𝗵𝗶𝘀 𝗩𝗶𝗱𝗲𝗼, 𝘆𝗼𝘂 𝘀𝗲𝗲 𝘁𝗵𝗮𝘁 𝗜 𝘁𝗲𝘀𝘁 𝘁𝗵𝗲 𝗽𝗲𝗿𝗳𝗼𝗿𝗺𝗮𝗻𝗰𝗲 𝗼𝗳 𝘁𝗵𝗲 𝗖𝗼𝗱𝗲 𝗮𝗻𝗱 𝗱𝗼𝘂𝗯𝗹𝗲-𝗰𝗵𝗲𝗰𝗸 𝗶𝘁 𝗹𝗮𝘁𝗲𝗿 𝘁𝗼 𝗯𝗲 𝘀𝘂𝗿𝗲 𝗮𝗯𝗼𝘂𝘁 𝗶𝘁.
Link to download the IOT MQTT Panel for android: Link
Below is the Code to be run on Python:
from ultralytics import YOLO
import cv2
import numpy as np
import paho.mqtt.client as mqtt
import time
# Load YOLOv8 model
model = YOLO("yolov8s.pt") # Use yolov8s.pt for better accuracy
# Open webcam
cap = cv2.VideoCapture("http://192.168.x.x:8000/stream.mjpg")
# Define the region of interest (ROI) using four points
roi_points = np.array([[0, 166], [603, 88], [606, 142], [0, 235]]) # Example coordinates
# Function to check if a point is inside a polygon
def is_point_in_polygon(x, y, polygon):
return cv2.pointPolygonTest(polygon, (x, y), False) >= 0
# Initialize car counter
car_count = 0
# initial the Mqqt server
broker = "test.mosquitto.org" # Use a free broker or your own
topic = "Define your own topic"
client = mqtt.Client(client_id="Desine your own ID")
client.connect(broker, 1883, 60)
# Ensure the MQTT client is connected before entering the loop
client.loop_start()
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Reset car counter for the current frame
car_count = 0
# Run YOLOv8 inference
results = model(frame)
# Define car class ID (based on COCO dataset)
car_class_id = 2 # Car
# Filter detections by polygonal ROI
for result in results:
for box in result.boxes:
if int(box.cls) == car_class_id:
# Get bounding box center coordinates
x_mid, y_mid, width, height = box.xywh[0]
x_center = float(x_mid)
y_center = float(y_mid)
# Check if the center of the bounding box is within the polygon
if is_point_in_polygon(x_center, y_center, roi_points):
print("Detected Car in ROI!")
car_count += 1
# Publish the car count to the MQTT topic
if car_count == 0:
park_value = str("There is 4 free parking lot")
elif car_count == 1:
park_value = str("There is 3 free parking lot")
elif car_count ==2:
park_value = str("There is 2 free parking lot")
elif car_count ==3:
park_value = str("There is 1 free parking lot")
else:
park_value = str("There is no free parking lot")
client.publish(topic, str(park_value))
# Draw the ROI polygon on the frame
cv2.polylines(frame, [roi_points], isClosed=True, color=(0, 255, 0), thickness=2)
# Print the number of cars detected
print(f"Number of cars detected in ROI: {car_count}")
# Display results
annotated_frame = results[0].plot()
cv2.imshow("YOLOv8 Car Detection", annotated_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
time.sleep(0)
# Stop the MQTT client loop
client.loop_stop()
cap.release()
cv2.destroyAllWindows()
I have tested this code and it works fine, however in it gets darker then its not so accurate so lets say until its bright enough this code will work. the other problem is that if someone park his car not in the correct spot then this code might not be able to deliver the number of free spot accurately. Remember that in IOT MQTT Panel software you need to write the Topic and client_id with “” otherwise it will not detect the value of the client!
Have fun with the code and use your Ip camera to detect the available free spot on the street Cheers 😀

Support
Please support me to write content for this Podcast. Thank you 🙂
€1.00





Leave a comment