做的是一个打开系统摄像头,并且进行目标检测的小demo,后期会出三个的结合,如有需要请关注三连,谢谢。
默认使用的是yolo11的权重模型文件,后期大家有自己更加精确的文件可以修改。
def camera_detection(self):"""摄像头检测"""try:model = YOLO('yolo11n.pt')camera = cv2.VideoCapture(0)if camera.isOpened():timer = QTimer()timer.timeout.connect(lambda: self.update_camera_frame(camera, model))timer.start(30)while True:if cv2.waitKey(1) & 0xFF == ord('q'):breakcamera.release()cv2.destroyAllWindows()else:print("无法打开摄像头,请检查设备是否连接正常。")except Exception as e:print(f"摄像头检测出现错误: {e}")
def update_camera_frame(self, camera, model):success, frame = camera.read()if success:results = model.predict(frame)annotated_frame = results[0].plot()cv2.imshow("YOLOv11 Inference", annotated_frame)else:print("摄像头读取失败,请检查设备是否正常工作。")
运行一下就能看见效果啦
if __name__ == "__main__":detector = Detector()detector.camera_detection()