Open rppycv opened 2 months ago
@rppycv hello!
Welcome to the world of computer vision! 😊 It looks like you're off to a great start with your custom YOLOv8 model. To detect when a new object appears and capture the time, you can modify your code slightly. Here's a suggestion:
datetime
to log the time when a new object is detected.Here's a modified version of your code to help you get started:
import cv2
from ultralytics import YOLO, solutions
import datetime
video_101 = "Camara_101_HD__20240812_15h05m.mp4"
def count_objects_in_region(video_path, output_video_path, model_path):
model = YOLO(model_path)
cap = cv2.VideoCapture(video_path)
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
region_points = [(430, 640), (780, 550), (740, 500), (410, 580)]
fourcc = int(cap.get(cv2.CAP_FFMPEG))
classes_to_count = [0, 1]
print(model.names)
video_writer = cv2.VideoWriter(output_video_path, fourcc, fps, (w, h))
counter = solutions.ObjectCounter(view_img=True, reg_pts=region_points, names=model.names, draw_tracks=True, line_thickness=2)
detected_objects = set()
while cap.isOpened():
success, im0 = cap.read()
if not success:
print("Video frame is empty or video processing has been successfully completed.")
break
tracks = model.track(im0, persist=True, show=False)
im0 = counter.start_counting(im0, tracks)
# Check for new objects
current_objects = set(track.id for track in tracks if track.cls in classes_to_count)
new_objects = current_objects - detected_objects
if new_objects:
detection_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"New object detected at {detection_time}: {new_objects}")
detected_objects.update(new_objects)
video_writer.write(im0)
cap.release()
video_writer.release()
cv2.destroyAllWindows()
count_objects_in_region(video_101, "{}___Camara101.mp4".format(datetime.datetime.now().strftime("%Y%m%d%H%M%S")), "best.pt")
This code will print the time whenever a new object is detected. Make sure to test it with the latest versions of the packages to ensure compatibility.
If you encounter any issues, feel free to reach out. The YOLO community and Ultralytics team are always here to help!
Happy coding! 🚀
Search before asking
Question
Hello All I'm new to working in computer vision (three months) and I'm looking for how to detect an object in a model trained with my own data, and its detection time.
I am using the following code from ultralytics, and it works fine with my custom model
1.- Code:
--- IMPORTAMOS LIBRERIAS ---
import cv2 from ultralytics import YOLO, solutions # error fbgemm.dll use #torch 2.3.0 and torchvision 0.18.0 import datetime # Se crea un objeto de tipo tiempo fechaActual = datetime.datetime.now() fechaFormat = fechaActual.strftime("%Y%m%d%H%M%S")
video_101 = "Camara_101_HD__20240812_15h05m.mp4"
def count_objects_in_region(video_path, output_video_path, model_path) """Count objects in a specific region within a video and call an API when an object is detected.""" model = YOLO(model_path) cap = cv2.VideoCapture(video_path) assert cap.isOpened(), "Error reading video file" w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS)) region_points = [(430, 640), (780, 550), (740, 500), (410, 580)] #FOR VIDEO HD
region_points = [(1270, 1920), (2340, 1650), (2220, 1520), (1220, 1770)] #stream RTSP
count_objects_in_region(video_101, "{}___Camara101.mp4".format(fechaFormat),"best.pt")
2.- I NEED!!! please When a new object of a class appears (there are 2 classes), detect it and get the time when it happened.
3.- Actual frame:
Additional
No response