###したいこと
Raspberry PIカメラモジュールを使ってカラートラッキングをし、ボールの重心を測定するコードになっています。
同じ事ができるなら多少コードを変えても構いません
コマンドラインでの実行ができない
Thonny上で実行はできるのですがコマンドライン上でpython Cam.pyを実行するとcv2.connectedComponentsWithStatsがエラーになってしまいます。なぜThonnyでは動作するのにコマンドラインでは動かないのでしょうか。
Python
1#!/usr/bin/python 2# -*- coding: utf-8 -*- 3 4from picamera import PiCamera 5from picamera.array import PiRGBArray 6import numpy as np 7import time 8import cv2 9 10#import threading 11 12CAMERA_WIDTH = 688 13CAMERA_HEIGHT = 480 14Xaxis = 10 15Yaxis = 10 16def centor(input): 17 if np.count_nonzero(input) <= 0: 18 return(-20,-20) 19 label = cv2.connectedComponentsWithStats(input) 20 n = label[0] -1 21 data = np.delete(label[2], 0, 0) 22 center = np.delete(label[3], 0, 0) 23 max_index = np.argmax(data[:,4]) 24 return center[max_index] 25 26camera = PiCamera() 27camera.resolution = (CAMERA_WIDTH, CAMERA_HEIGHT) 28camera.framerate = 30 29rawCapture = PiRGBArray(camera, size=(CAMERA_WIDTH, CAMERA_HEIGHT)) 30time.sleep(0.1) 31for image in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): 32 stream = image.array 33 #data = np.fromstring(stream.getvalue(), dtype=np.uint8) 34 #image = cv2.imdecode(data, 1) 35 hsv = cv2.cvtColor(stream,cv2.COLOR_BGR2HSV) 36 lower_blue = np.array([160,100,100]) 37 upper_blue = np.array([180,200,200]) 38 mask = cv2.inRange(hsv,lower_blue, upper_blue) 39 denoize = cv2.medianBlur(mask,5) 40 Xaxis,Yaxis = centor(mask) 41 print(Xaxis,Yaxis) 42 #cv2.circle(mask, (int(Xaxis), int(Yaxis)), 10, (0, 0, 255), 5) 43 cv2.circle(stream, (int(Xaxis), int(Yaxis)), 20, (255, 255, 0), 10) 44 #cv2.imshow('image',mask) 45 cv2.imshow('Mix',stream) 46 key = cv2.waitKey(1) & 0xFF 47 48 rawCapture.truncate(0) 49 50 if key == ord("q"): 51 break 52cv2.destroyAllWindows()
エラー
mmal: mmal_vc_port_enable: failed to enable port vc.null_sink:in:0(OPQV): ENOSPC mmal: mmal_port_enable: failed to enable connected port (vc.null_sink:in:0(OPQV))0x12e2e10 (ENOSPC) mmal: mmal_connection_enable: output port couldn't be enabled Traceback (most recent call last): File "Cam.py", line 26, in <module> camera = PiCamera() File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 433, in __init__ self._init_preview() File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 513, in _init_preview self, self._camera.outputs[self.CAMERA_PREVIEW_PORT]) File "/usr/lib/python2.7/dist-packages/picamera/renderers.py", line 558, in __init__ self.renderer.inputs[0].connect(source).enable() File "/usr/lib/python2.7/dist-packages/picamera/mmalobj.py", line 2212, in enable prefix="Failed to enable connection") File "/usr/lib/python2.7/dist-packages/picamera/exc.py", line 184, in mmal_check raise PiCameraMMALError(status, prefix) picamera.exc.PiCameraMMALError: Failed to enable connection: Out of resources
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/25 13:40