You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
12 Hrs
44 Min
35 Seg

Configuraci贸n y Uso de Modelos YOLO Preentrenados

8/16
Resources

Object detection is one of the most fascinating and useful applications of computer vision. With the advancement of pre-trained models such as YOLO (You Only Look Once), it is now possible to implement robust detection systems with relative ease. In this content, we will explore how to create our first object detector using YOLOv11, a model capable of identifying up to 80 different classes of objects in real time.

How to implement an object detector with YOLOv11?

To start working with object detection, we need to configure our environment with the appropriate libraries. The YOLO model in its version 11 allows us to detect a wide variety of objects such as books, cups, cars and teddy bears, among others. The accuracy of the detection will depend on the angle and conditions in which the image is captured.

For the implementation we will need:

  • OpenCV for image and video processing.
  • Time to measure the latency between frames
  • Ultralytics to load the YOLO model.

Initial configuration of the detector

The first step is to load the pre-trained model and configure the video capture:

# We import the necessary librariesimport cv2import timefrom ultralytics import YOLO
 # We load the YOLOv11 model model = YOLO('yolov11n.pt')
 # We start the video capturecap = cv2.VideoCapture(1) # Use 0 if there is only one camera
 # Set the window size for full screencap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

Once the environment is set up, we can start processing each frame of the video to detect objects. It is important to measure the processing time between frames to evaluate the performance of our system.

Frame processing and object detection

The next step is to capture each frame, process it with the YOLO model and visualize the results:

while True: # We capture the frame ret, frame = cap.read() if not ret: break    
 # We measure the time before processing start_time = time.time()    
 # We process the frame with YOLO results = model(frame)    
 # We calculate the latency latency = time.time() - start_time    
 # We access the first result result = results[0]    
 # We get the detections (boxes) boxes = result.boxes    
 # Process each detection for box in boxes: # Get the coordinates of the bounding box x1, y1, x2, y2 = box.xyxy[0] x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2), int(y2)        
 # Get the confidence and class confidence = box.conf[0] class_id = int(box.cls[0]) class_name = result.names[class_id]        
 # We draw the rectangle and label cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2) label = f"{class_name}: {confidence:.2f}" cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 0), 2)    
 # Show latency in frame cv2.putText(frame, f "Latency: {latency:.4f}s", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0, 255), 2)    
 # Show frame cv2.imshow("Object Detection", frame)    
 # Exit with key 'q' if cv2.waitKey(1) == ord('q'): break
cap.release()cv2.destroyAllWindows()

In this code, for each detected object, we obtain:

  • The coordinates of the bounding box (x1, y1, x2, y2).
  • The confidence level of the detection
  • The class to which the object belongs

The bounding box is defined by two coordinates: the upper left (x1, y1) and the lower right (x2, y2). These coordinates allow us to draw a rectangle around the detected object.

How to filter specific detections?

A very useful feature is the ability to filter the detections to show only the objects we are interested in. For example, if we only want to detect people, we can modify our code to filter by the class "person" (which has ID 0 in YOLO) and set a minimum confidence threshold.

Filtering by class and confidence

# We define the class we want to detect (0 = person)target_class = 0
 # We define the confidence thresholdconfidence_threshold = 0.7
 # In the processing loop, we modify the detection part:for box in boxes: # We get the confidence and the class confidence = box.conf[0] class_id = int(box.cls[0])    
 # We only process if it is the target class and exceeds the confidence threshold if class_id == target_class and confidence >= confidence_threshold: # We get the coordinates of the bounding box x1, y1, x2, y2 = box.xyxy[0] x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2), int(y2)        
 class_name = result.names[class_id]        
 # Draw the rectangle and label cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2) label = f"{class_name}: {confidence:.2f}" cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

With this modification, our detector will only show people with a confidence level higher than 0.7 (70%). This is especially useful in applications where we are only interested in a specific type of object and we want to avoid false positives.

What objects can YOLOv11 detect?

The YOLOv11 model comes pre-trained to detect 80 different classes of objects. Some of these classes include:

  • People (ID 0)
  • Vehicles: cars, trucks, boats
  • Animals: dogs, cats, horses, etc.
  • Everyday objects: mugs, books, telephones
  • Food: various types of food

This versatility makes YOLO a powerful tool for a wide range of computer vision applications.

Object detection with YOLO opens up a world of possibilities in diverse industries, from security and surveillance to industrial automation and behavioral analysis. With the code we've explored, you now have the foundation to implement your own object detection system. What practical applications can you imagine for this technology in your field? Share your ideas in the comments.

Contributions 1

Questions 1

Sort by:

Want to see more contributions, questions and answers from the community?

Comparto todas las clases : <https://gist.github.com/rcland12/dc48e1963268ff98c8b2c4543e7a9be8> no colocaron el enlace.. ; )