{"id":2383,"date":"2019-01-06T10:58:10","date_gmt":"2019-01-06T01:58:10","guid":{"rendered":"http:\/\/research.itplants.com\/?p=2383"},"modified":"2019-01-06T11:26:01","modified_gmt":"2019-01-06T02:26:01","slug":"picam%e6%9c%80%e9%80%9f%e3%82%ad%e3%83%a3%e3%83%97%e3%83%81%e3%83%a3","status":"publish","type":"post","link":"https:\/\/research.itplants.com\/?p=2383","title":{"rendered":"PiCam\u6700\u901f\u30ad\u30e3\u30d7\u30c1\u30e3"},"content":{"rendered":"<p>Raspberry Pi\u306ePiCam\u306f\u3001\u3082\u306e\u51c4\u304f\u65e9\u3044\u30ad\u30e3\u30d7\u30c1\u30e3\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>640X480\u3067\u3001\u306a\u3093\u3068\u3001 946.946038832 fps\u304c\u3067\u307e\u3057\u305f\u3002\u4eca\u307e\u3067\u306b\u898b\u305f\u3053\u3068\u306e\u306a\u3044\u30d3\u30c7\u30aa\u30ad\u30e3\u30d7\u30c1\u30e3\u6027\u80fd\u3067\u3059\u3002\u305d\u306e\u65b9\u6cd5\u306f\u3001<code>raspividyuv<\/code>\u3092\u30b5\u30d6\u30d7\u30ed\u30bb\u30b9\u3067\u52d5\u4f5c\u3055\u305b\u308b\u3060\u3051\u3067\u3059\u3002<\/p>\n<p><code>raspividyuv -w 640 -h 480 --output - --timeout 0 --framerate 250 --nopreview<\/code><\/p>\n<p>\u306e\u6761\u4ef6\u304c\u6700\u901f\u3067\u3057\u305f\u3002PiCam\u3063\u3066\u3001\u3059\u3054\u3044\u3093\u3067\u3059\u306d\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/gist.github.com\/Eric013\/ceaf59bbb209bfeb29c97e3e776f1d19\">\u51fa\u5178\u30b5\u30a4\u30c8<\/a><\/p>\n<p><code>#!\/usr\/bin\/env python<br \/>\n# Fast reading from the raspberry camera with Python, Numpy, and OpenCV<br \/>\n# Allows to process grayscale video up to 124 FPS (tested in Raspberry Zero Wifi with V2.1 camera)<br \/>\n#<br \/>\n# Made by @CarlosGS in May 2017<br \/>\n# Club de Robotica - Universidad Autonoma de Madrid<br \/>\n# http:\/\/crm.ii.uam.es\/<br \/>\n# License: Public Domain, attribution appreciated<br \/>\nimport cv2<br \/>\nimport numpy as np<br \/>\nimport subprocess as sp<br \/>\nimport time<br \/>\nimport atexit<br \/>\nframes = [] # stores the video sequence for the demo<br \/>\nmax_frames = 300<br \/>\nN_frames = 0<br \/>\n# Video capture parameters<br \/>\n(w,h) = (640,240)<br \/>\nbytesPerFrame = w * h<br \/>\nfps = 250 # setting to 250 will request the maximum framerate possible<br \/>\n#fps = 30 # setting to 250 will request the maximum framerate possible<br \/>\n# \"raspividyuv\" is the command that provides camera frames in YUV format<br \/>\n# \"--output -\" specifies stdout as the output<br \/>\n# \"--timeout 0\" specifies continuous video<br \/>\n# \"--luma\" discards chroma channels, only luminance is sent through the pipeline<br \/>\n# see \"raspividyuv --help\" for more information on the parameters<br \/>\n#videoCmd = \"raspividyuv -w \"+str(w)+\" -h \"+str(h)+\" --output - --timeout 0 --framerate \"+str(fps)+\" --luma --nopreview\"<br \/>\n#videoCmd = \"raspividyuv -w \"+str(w)+\" -h \"+str(h)+\" --output - --timeout 0 --framerate \"+str(fps)+\" --nopreview\"<br \/>\n#videoCmd = \"raspivid -w \"+str(w)+\" -h \"+str(h)+\" --output - --timeout 0 --framerate \"+str(fps)+\" --nopreview\"<br \/>\nvideoCmd = \"raspividyuv -w 1920 -h 1080 --output - --timeout 0 --framerate \"+str(fps)+\" --nopreview\"<br \/>\nvideoCmd = videoCmd.split() # Popen requires that each parameter is a separate string<br \/>\ncameraProcess = sp.Popen(videoCmd, stdout=sp.PIPE) # start the camera<br \/>\natexit.register(cameraProcess.terminate) # this closes the camera process in case the python scripts exits unexpectedly<br \/>\n# wait for the first frame and discard it (only done to measure time more accurately)<br \/>\nrawStream = cameraProcess.stdout.read(bytesPerFrame)<br \/>\nprint(\"Recording...\")<br \/>\nstart_time = time.time()<br \/>\nwhile True:<br \/>\ncameraProcess.stdout.flush() # discard any frames that we were not able to process in time<br \/>\n# Parse the raw stream into a numpy array<br \/>\nframe = np.fromfile(cameraProcess.stdout, count=bytesPerFrame, dtype=np.uint8)<br \/>\nif frame.size != bytesPerFrame:<br \/>\nprint(\"Error: Camera stream closed unexpectedly\")<br \/>\nbreak<br \/>\nframe.shape = (h,w) # set the correct dimensions for the numpy array<br \/>\n# The frame can be processed here using any function in the OpenCV library.<br \/>\n# Full image processing will slow down the pipeline, so the requested FPS should be set accordingly.<br \/>\n#frame = cv2.Canny(frame, 50,150)<br \/>\n# For instance, in this example you can enable the Canny edge function above.<br \/>\n# You will see that the frame rate drops to ~35fps and video playback is erratic.<br \/>\n# If you then set fps = 30 at the beginning of the script, there will be enough cycle time between frames to provide accurate video.<br \/>\n# One optimization could be to work with a decimated (downscaled) version of the image: deci = frame[::2, ::2]<br \/>\nframes.append(frame) # save the frame (for the demo)<br \/>\n#del frame # free the allocated memory<br \/>\nN_frames += 1<br \/>\nif N_frames &gt; max_frames: break<br \/>\nend_time = time.time()<br \/>\ncameraProcess.terminate() # stop the camera<br \/>\nelapsed_seconds = end_time-start_time<br \/>\nprint(\"Done! Result: \"+str(N_frames\/elapsed_seconds)+\" fps\")<br \/>\nprint(\"Writing frames to disk...\")<br \/>\nout = cv2.VideoWriter(\"slow_motion.avi\", cv2.VideoWriter_fourcc(*'MJPG'), 30, (w,h))<br \/>\nfor n in range(N_frames):<br \/>\n#cv2.imwrite(\"frame\"+str(n)+\".png\", frames[n]) # save frame as a PNG image<br \/>\nframe_rgb = cv2.cvtColor(frames[n],cv2.COLOR_GRAY2RGB) # video codec requires RGB image<br \/>\nout.write(frame_rgb)<br \/>\nout.release()<br \/>\nprint(\"Display frames with OpenCV...\")<br \/>\nfor frame in frames:<br \/>\ncv2.imshow(\"Slow Motion\", frame)<br \/>\ncv2.waitKey(1) # request maximum refresh rate<br \/>\ncv2.destroyAllWindows()<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Raspberry Pi\u306ePiCam\u306f\u3001\u3082\u306e\u51c4\u304f\u65e9\u3044\u30ad\u30e3\u30d7\u30c1\u30e3\u304c\u3067\u304d\u307e\u3059\u3002 640X480\u3067\u3001\u306a\u3093\u3068\u3001 946.946038832 fps\u304c\u3067\u307e\u3057\u305f\u3002\u4eca\u307e\u3067\u306b\u898b\u305f\u3053\u3068\u306e\u306a\u3044\u30d3\u30c7\u30aa\u30ad\u30e3\u30d7\u30c1\u30e3\u6027\u80fd\u3067\u3059\u3002\u305d\u306e\u65b9\u6cd5\u306f\u3001raspi&#8230;<\/p>\n<p><a class=\"more\" href=\"https:\/\/research.itplants.com\/?p=2383\"> Read more &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"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\/2383"}],"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=2383"}],"version-history":[{"count":5,"href":"https:\/\/research.itplants.com\/index.php?rest_route=\/wp\/v2\/posts\/2383\/revisions"}],"predecessor-version":[{"id":2388,"href":"https:\/\/research.itplants.com\/index.php?rest_route=\/wp\/v2\/posts\/2383\/revisions\/2388"}],"wp:attachment":[{"href":"https:\/\/research.itplants.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/research.itplants.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/research.itplants.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}