DIY人脸跟踪电风扇送女朋友(4)
生活随笔
收集整理的這篇文章主要介紹了
DIY人脸跟踪电风扇送女朋友(4)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
成果展示:
如下視頻所示為制作好的人臉跟蹤電風扇
人臉跟蹤風扇
?
相關代碼已經放在百度網盤上面了。
鏈接:https://pan.baidu.com/s/188AylEd2QgOiA6YFv01N3A
提取碼:e2og
?
主要原理
其主要原理就是,攝像頭對風扇前面的內容進行預覽,樹莓派對攝像頭拍到的畫面進行實時的檢測,檢測出人臉的位置。然后根據位置來控制云臺的轉動,從而達到風扇始終對著人臉的目標。
?
原理示意圖:
樹莓派通過檢測人臉位置,大致判斷人臉相對自己的位置,然后轉換成角度,控制云臺運動。
?
?
?
?
代碼編寫:
這里主要看一下最重要的代碼,就是在獲取到一幀圖像之后的處理流程,我把它放在slot_camera方法中
1 def slot_camera(self):2 if (self.cap.isOpened()):3 self.frame += 14 # get a frame5 ret, img = self.cap.read()6 height, width, bytesPerComponent = img.shape7 bytesPerLine = bytesPerComponent * width8 9 if self.frame % 1 == 0:10 image_p = img.ctypes.data_as(ctypes.c_char_p)11 self.alg_handle.alg_run.restype = ctypes.POINTER(ctypes.c_float)12 result = self.alg_handle.alg_run(image_p, 11, height, width, 4)13 14 # cls15 if int(result[0]) == 0:16 pass17 # det18 elif int(result[0]) == 1:19 box_count = int(result[1])20 for j in range(box_count):21 cls = int(result[2 + j * 6 + 0])22 prob = float(result[2 + j * 6 + 1])23 if prob < 0.4:24 continue25 x1 = int(float(result[2 + j * 6 + 2]) * width)26 y1 = int(float(result[2 + j * 6 + 3]) * height)27 x2 = int(float(result[2 + j * 6 + 4]) * width)28 y2 = int(float(result[2 + j * 6 + 5]) * height)29 cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 1)30 x_center = (float(result[2 + j * 6 + 2]) + float(result[2 + j * 6 + 4])) / 231 y_center = (float(result[2 + j * 6 + 3]) + float(result[2 + j * 6 + 5])) / 232 x_angle = int((x_center - 0.5) * 40 / 0.5)33 y_angle = int((y_center - 0.5) * 40 / 0.5)34 self.servo_cmd['x_dir'] = 'left' if x_angle <= 0 else 'right'35 self.servo_cmd['x_angle'] = abs(x_angle)36 37 cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img)38 cv2.putText(img, 'x:%s %d'%( self.servo_cmd['x_dir'], self.servo_cmd['x_angle']), (10, 40), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 0, 255), 2)39 ServoControl('x', self.servo_cmd['x_dir'], self.servo_cmd['x_angle'])40 41 self.image = QImage(img.data, width, height, bytesPerLine, QImage.Format_RGB888)42 self.label_frame.setPixmap(QPixmap.fromImage(self.image).scaled(self.label_frame.width(), self.label_frame.height()))43 44?
總結
以上是生活随笔為你收集整理的DIY人脸跟踪电风扇送女朋友(4)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 美国两架波音客机在机场相撞:机翼都断了
- 下一篇: 小米 6 成功移植 OpenHarmon