有物体时如何转动舵机?

问题描述 投票:0回答:1

目前我正在树莓派上使用 yolov8 进行对象检测。我的项目是检测这三种物体(铝罐、塑料瓶、玻璃瓶)。我已经训练了数据集并导出为 best.pt 文件。

现在我陷入了检测到物体时如何旋转伺服器。这是我的代码。

from ultralytics import YOLO
import cv2
from ultralytics.yolo.utils.plotting import Annotator
import torch
from gpiozero import Servo
from time import sleep
from gpiozero.pins.pigpio import PiGPIOFactory

factory = PiGPIOFactory()

servo = Servo(12, min_pulse_width=0.5/1000, max_pulse_width=2.5/1000, pin_factory=factory)

model = YOLO('best.pt')
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)

while True:
_, frame = cap.read()

img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

results = model.predict(img)

for r in results:
    
    annotator = Annotator(frame)
    
    boxes = r.boxes
    for box in boxes:
        
        b = box.xyxy[0]  # get box coordinates in (top, left, bottom, right) format
        c = box.cls
        annotator.box_label(b, model.names[int(c)])
       
      
frame = annotator.result()
  
cv2.imshow('YOLO V8 Obj Detection', frame)

#servo part

if cv2.waitKey(1) & 0xFF == ord(' '):
    break

cap.release()
cv2.destroyAllWindows()
python raspberry-pi raspberry-pi4 robotics
1个回答
0
投票

你找到答案了吗?如果你发现了请告诉我。请

© www.soinside.com 2019 - 2024. All rights reserved.