ultimatemasashi が 2021年01月03日01時20分38秒 に編集
初版
タイトルの変更
顔で動かすマウス
タグの変更
秋葉原2021
メイン画像の変更
本文の変更
 画像認識アプリケーションであるOpenCVで顔を読み取りサーボモーターを動かしてマウスポインタを動かします。  3Dプリンタで作成したケースにはピンポン玉が入っており縦横にサーボモーターを動かすことにより上下左右にポインタを動かす事ができます。  pythonコード #! /usr/bin/env python import cv2 import time import Adafruit_PCA9685 pwm = Adafruit_PCA9685.PCA9685() face_cascade_path = 'haarcascade_frontalface_default.xml' eye_cascade_path = 'haarcascade_eye.xml' face_cascade = cv2.CascadeClassifier(face_cascade_path) eye_cascade = cv2.CascadeClassifier(eye_cascade_path) cap = cv2.VideoCapture(0) while True: pwm.set_pwm_freq(60) ret, img = cap.read() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5) for x, y, w, h in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) face = img[y: y + h, x: x + w] face_gray = gray[y: y + h, x: x + w] eyes = eye_cascade.detectMultiScale(face_gray) for (ex, ey, ew, eh) in eyes: cv2.rectangle(face, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2) print(x ,"x") print(y ,"Y") if x > 200 : pwm.set_pwm(15, 0,200) time.sleep(0.05)pwm.set_pwm(15, 0, 0) print("右") if x < 100 : pwm.set_pwm(15, 0,700) time.sleep(0.05) pwm.set_pwm(15, 0, 0) print("左") if y > 200 : pwm.set_pwm(14, 0, 700) time.sleep(0.05) pwm.set_pwm(14, 0, 0) print("上") if y < 100 : pwm.set_pwm(14, 0, 200) time.sleep(0.05) pwm.set_pwm(14, 0, 0) print("下") cv2.imshow('video image', img) key = cv2.waitKey(10) if key == 27: break cap.release() cv2.destroyAllWindows() ・材料 raspberry pi 4 pca9685 360°サーボモーター ピンポン玉 ウェブカメラ 光学マウス 架台(3Dプリンタで作成) これで腕を動かさなくてもマウスポインタが動かせます!!