{"id":1562,"date":"2017-07-20T16:43:56","date_gmt":"2017-07-20T07:43:56","guid":{"rendered":"http:\/\/research.itplants.com\/?p=1562"},"modified":"2017-07-20T16:43:56","modified_gmt":"2017-07-20T07:43:56","slug":"picam%e3%81%ae%e9%ab%98%e9%80%9f%e5%8c%96","status":"publish","type":"post","link":"https:\/\/research.itplants.com\/?p=1562","title":{"rendered":"PiCam\u306e\u9ad8\u901f\u5316"},"content":{"rendered":"<p>RaspberryPi\u306epicamera\u306f\u3001\u5c0f\u3055\u304f\u3066\u512a\u79c0\u3067\u3059\u304c\u3001\u8aad\u307f\u8fbc\u307f\u901f\u5ea6\u304c\u3042\u307e\u308a\u65e9\u304f\u306a\u3044\u306a\u3063\u3066\u601d\u3063\u3066\u3044\u307e\u3057\u305f\u3002\u3075\u3068\u3001Therad\u5316\u3059\u308c\u3070\u3001\u65e9\u304f\u306a\u308b\u3093\u3058\u3083\u306a\u3044\u304b\u3063\u3066\u8003\u3048\u3066\u8abf\u3079\u308b\u3068<a href=\"http:\/\/www.pyimagesearch.com\/2015\/12\/28\/increasing-raspberry-pi-fps-with-python-and-opencv\/\" target=\"_blank\" rel=\"noopener\">\u3042\u308a\u307e\u3057\u305f<\/a>\u3002<\/p>\n<p>Therad\u5316\u3059\u308b\u3053\u3068\u3067\u3001\u30ab\u30e1\u30e9\u753b\u50cf\u306e\u8aad\u8fbc\u304c\uff13\uff0c\uff14\u500d\u65e9\u304f\u306a\u308a\u307e\u3059\u3002\u4f7f\u3044\u65b9\u3082\u7c21\u5358\u3067\u3059\u3002\u307b\u307c\u3001\u30b9\u30c8\u30ec\u30b9\u306a\u304f\u52d5\u304f\u3088\u3046\u306b\u898b\u3048\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# import the necessary packages\r\nfrom picamera.array import PiRGBArray\r\nfrom picamera import PiCamera\r\nfrom threading import Thread\r\nimport cv2\r\nimport time\r\n\r\nclass PiVideoStream:\r\n\tdef __init__(self, resolution=(320, 240), framerate=32):\r\n\t\t# initialize the camera and stream\r\n\t\tself.camera = PiCamera()\r\n\t\tself.camera.resolution = resolution\r\n\t\tself.camera.framerate = framerate\r\n\t\tself.rawCapture = PiRGBArray(self.camera, size=resolution)\r\n\t\tself.stream = self.camera.capture_continuous(self.rawCapture, format=&quot;bgr&quot;, use_video_port=True)\r\n\t\tself.stopped=False\r\n\r\n\t\t# initialize the frame and the variable used to indicate\r\n\t\t# if the thread should be stopped\r\n\t\tself.frame = None\r\n\t\tself.stopped = False\r\n\t\t#self.camera.resolution = (640, 480)\r\n\t\t#self.camera.framerate = 32\r\n\t\tself.camera.sharpness = 0\r\n\t\tself.camera.contrast = 0\r\n\t\tself.camera.brightness = 50\r\n\t\tself.camera.saturation = 0\r\n\t\tself.camera.ISO = 0\r\n\t\tself.camera.video_stabilization = False\r\n\t\tself.camera.exposure_compensation = 0\r\n\t\t#self.camera.exposure_mode = 'off'\r\n\t\tself.camera.awb_mode = 'flash'\r\n\t\tself.camera.meter_mode = 'average'\r\n\t\tself.camera.image_effect = 'none'\r\n\t\tself.camera.color_effects = None\r\n\t\tself.camera.rotation = 0\r\n\t\tself.camera.hflip = True\r\n\t\tself.camera.vflip = True\r\n\t\tself.camera.crop = (0.0, 0.0, 1.0, 1.0)\r\n\r\n\tdef start(self):\r\n\t\t# start the thread to read frames from the video stream\r\n\t\tThread(target=self.update, args=()).start()\r\n\t\treturn self\r\n\r\n\tdef update(self):\r\n\t\t# keep looping infinitely until the thread is stopped\r\n\t\tfor f in self.stream:\r\n\t\t\t# grab the frame from the stream and clear the stream in\r\n\t\t\t# preparation for the next frame\r\n\t\t\tself.frame = f.array\r\n\t\t\tself.rawCapture.truncate(0)\r\n\r\n\t\t\t# if the thread indicator variable is set, stop the thread\r\n\t\t\t# and resource camera resources\r\n\t\t\tif self.stopped:\r\n\t\t\t\tself.stream.close()\r\n\t\t\t\tself.rawCapture.close()\r\n\t\t\t\tself.camera.close()\r\n\t\t\t\treturn\r\n\r\n\tdef read(self):\r\n\t\t# return the frame most recently read\r\n\t\treturn self.frame\r\n\r\n\tdef stop(self):\r\n\t\t# indicate that the thread should be stopped\r\n\t\tself.stopped = True\r\n\r\n\tdef seek(self):\r\n\t\tself.rawCapture.seek(-1,2)\r\n\r\n\r\nvs = PiVideoStream((640,480),32).start()\r\ntime.sleep(2.0) \r\n\r\ndef PiCamCapture():\r\n        return vs.read()\r\n\r\ndef PiCamCaptureSeek():\r\n        return vs.seek()\r\n\r\ndef PiCamCaptureStop():\r\n        vs.stop()\r\n\t\r\nif __name__ == '__main__':\r\n\t#\r\n\tcv2.namedWindow('Frame', cv2.WINDOW_NORMAL)\r\n\twhile True:\r\n        \tframe = PiCamCapture()\r\n\r\n        \t# check to see if the frame should be displayed to our screen\r\n                cv2.imshow(&quot;Frame&quot;, frame)\r\n               \tkey = cv2.waitKey(1) &amp; 0xFF\r\n               \tif key == 27:\r\n                       \tbreak\r\n\r\n\r\n\t# do a bit of cleanup\r\n       \tcv2.destroyAllWindows()\r\n\tPiCamCaptureStop()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>RaspberryPi\u306epicamera\u306f\u3001\u5c0f\u3055\u304f\u3066\u512a\u79c0\u3067\u3059\u304c\u3001\u8aad\u307f\u8fbc\u307f\u901f\u5ea6\u304c\u3042\u307e\u308a\u65e9\u304f\u306a\u3044\u306a\u3063\u3066\u601d\u3063\u3066\u3044\u307e\u3057\u305f\u3002\u3075\u3068\u3001Therad\u5316\u3059\u308c\u3070\u3001\u65e9\u304f\u306a\u308b\u3093\u3058\u3083\u306a\u3044\u304b\u3063\u3066\u8003\u3048\u3066\u8abf\u3079\u308b\u3068\u3042\u308a\u307e\u3057\u305f\u3002 Therad\u5316\u3059\u308b\u3053\u3068\u3067\u3001&#8230;<\/p>\n<p><a class=\"more\" href=\"https:\/\/research.itplants.com\/?p=1562\"> Read more &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":1567,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/research.itplants.com\/index.php?rest_route=\/wp\/v2\/posts\/1562"}],"collection":[{"href":"https:\/\/research.itplants.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/research.itplants.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/research.itplants.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/research.itplants.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1562"}],"version-history":[{"count":5,"href":"https:\/\/research.itplants.com\/index.php?rest_route=\/wp\/v2\/posts\/1562\/revisions"}],"predecessor-version":[{"id":1568,"href":"https:\/\/research.itplants.com\/index.php?rest_route=\/wp\/v2\/posts\/1562\/revisions\/1568"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/research.itplants.com\/index.php?rest_route=\/wp\/v2\/media\/1567"}],"wp:attachment":[{"href":"https:\/\/research.itplants.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/research.itplants.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/research.itplants.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}