from __future__ import division import time import os,sys #sys.path.append(os.getcwd()) #from Adafruit_Python_PCA9685.Adafruit_PCA9685 import Adafruit_PCA9685 #from Adafruit_Python_PCA9685 import Adafruit_PCA9685 import Adafruit_PCA9685 pwm = Adafruit_PCA9685.PCA9685() pwm.set_pwm_freq(50) import cv2 print(cv2) import sys import cv2.cv as cv #import cv2 #import cv2 as cv from optparse import OptionParser def move(degree_1,degree_2): degree_1 = int(degree_1 * 5.27) degree_2 = int(degree_2 * 5.27) pwm.set_pwm(0, 0, degree_1) pwm.set_pwm(1, 0, degree_2) # Parameters for haar detection # From the API: # The default parameters (scale_factor=2, min_neighbors=3, flags=0) are tuned # for accurate yet slow object detection. For a faster operation on real video # images the settings are: # scale_factor=1.2, min_neighbors=2, flags=CV_HAAR_DO_CANNY_PRUNING, # min_size=<minimum possible face size min_size = (20, 20) image_scale = 2 haar_scale = 1.2 min_neighbors = 2 haar_flags = 0 def detect_and_draw(img, cascade): # allocate temporary images gray = cv.CreateImage((img.width,img.height), 8, 1) small_img = cv.CreateImage((cv.Round(img.width / image_scale), cv.Round (img.height / image_scale)), 8, 1) # convert color input image to grayscale cv.CvtColor(img, gray, cv.CV_BGR2GRAY) # scale input image for faster processing cv.Resize(gray, small_img, cv.CV_INTER_LINEAR) cv.EqualizeHist(small_img, small_img) if(cascade): t = cv.GetTickCount() faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) t = cv.GetTickCount() - t print "detection time = %gms" % (t/(cv.GetTickFrequency()*1000.)) if faces: for ((x, y, w, h), n) in faces: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints pt1 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0) cv.ShowImage("result", img) if __name__ == '__main__': print('move') move(60,60) time.sleep(1) move(65,60) time.sleep(1) move(65,65) time.sleep(1) move(60,65) time.sleep(1) move(60,55) time.sleep(1) print('stop') print('koko') parser = OptionParser(usage = "usage: %prog [options] [filename|camera_index]") parser.add_option("-c", "--cascade", action="store", dest="cascade", type="str", help="Haar cascade file, default %default", default = "../data/haarcascades/haarcascade_frontalface_alt.xml") (options, args) = parser.parse_args() cascade = cv.Load(options.cascade) if len(args) != 1: parser.print_help() sys.exit(1) print('koko1') input_name = args[0] if input_name.isdigit(): capture = cv.CreateCameraCapture(int(input_name)) print('koko222') else: capture = None print('koko2') print('abspath: ', os.path.abspath(__file__)) print('abs dirname: ', os.path.dirname(os.path.abspath(__file__))) print('koko3') cv.NamedWindow("result",0) #cv.NamedWindow("aresult") print('koko4') width = 320 #leave None for auto-detection height = 240 #leave None for auto-detection if width is None: width = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH)) else: cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH,width) if height is None: height = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT)) else: cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT,height) if capture: frame_copy = None while True: frame = cv.QueryFrame(capture) if not frame: cv.WaitKey(0) break if not frame_copy: frame_copy = cv.CreateImage((frame.width,frame.height), cv.IPL_DEPTH_8U, frame.nChannels) # frame_copy = cv.CreateImage((frame.width,frame.height), # cv.IPL_DEPTH_8U, frame.nChannels) if frame.origin == cv.IPL_ORIGIN_TL: cv.Copy(frame, frame_copy) else: cv.Flip(frame, frame_copy, 0) detect_and_draw(frame_copy, cascade) kuri=1 print('move') move(60+kuri,60+kuri) time.sleep(1) kuri=-5 move(60+kuri,60+kuri) time.sleep(1) if cv.WaitKey(10) >= 0: break else: image = cv.LoadImage(input_name, 1) detect_and_draw(image, cascade) cv.WaitKey(0) cv.DestroyWindow("result") ```### 前提・実現したいこと Raspbery-Piで、顔画像を追いかけるように、カメラを動かそうとしています。sudo実行では、カメラをサーボモータで動かせるが、ビデオが作成されません。sudo無しで実行すると、サーボ駆動ができなくなります。 ### 発生している問題・エラーメッセージ sudo実行で、 SUDO python m-facedtect.py --cascae=face.xml 0 だと、下記のエラーです。 Client is not authorized to connect to Server (result:3174): Gtk-WARNING **: cannot open display: :0.0 調べると、import cv2cv as cv としていて、コードが、 cv.NamedWindow("result",0) のところです。 sudoなしの実行で、python m-facedtect.py --cascae=face.xml 0 だと、下記のエラーです。 Traceback (most recent call last): File "m-facedetect.py", line 12, in <module> pwm = Adafruit_PCA9685.PCA9685() File "/usr/local/lib/python2.7/dist-packages/Adafruit_PCA9685-1.0.1-py2.7.egg/Adafruit_PCA9685/PCA9685.py", line 75, in __init__ self._device = i2c.get_i2c_device(address, **kwargs) File "/usr/local/lib/python2.7/dist-packages/Adafruit_GPIO-1.0.3-py2.7.egg/Adafruit_GPIO/I2C.py", line 65, in get_i2c_device return Device(address, busnum, i2c_interface, **kwargs) File "/usr/local/lib/python2.7/dist-packages/Adafruit_GPIO-1.0.3-py2.7.egg/Adafruit_GPIO/I2C.py", line 98, in __init__ self._bus = Adafruit_PureIO.smbus.SMBus(busnum) File "/usr/local/lib/python2.7/dist-packages/Adafruit_PureIO-0.2.1-py2.7.egg/Adafruit_PureIO/smbus.py", line 97, in __init__ self.open(bus) File "/usr/local/lib/python2.7/dist-packages/Adafruit_PureIO-0.2.1-py2.7.egg/Adafruit_PureIO/smbus.py", line 122, in open self._device = open('/dev/i2c-{0}'.format(bus), 'r+b', buffering=0) IOError: [Errno 13] Permission denied: '/dev/i2c-1' ### 該当のソースコード ### 試したこと sudo に、cv2.cv の利用権限を、与えなおそうとした。 sudo -R root /usr/lib/pymodules/python2.7/cv2.so しかし、/usr/lib/pymodules/python2.7/cv2.so がないというメッセージがでた。 この場所は、 import cv2 print(cv2)で、返ってくる、場所で、実際にあります。
回答1件
あなたの回答
tips
プレビュー