官术网_书友最值得收藏!

Saving camera frames

This previous example can be easily modified to add a useful functionality. Imagine that you want to save some frames to disk when something interesting happens. In the following example, read_camera_capture.py, we are going to add this functionality. When the C key is pressed on the keyboard, we save the current frame to disk. We save both the BGR and the grayscale frames. The code that performs this functionality is shown here:

 # Press c on keyboard to save current frame
if cv2.waitKey(20) & 0xFF == ord('c'):
frame_name = "camera_frame_{}.png".format(frame_index)
gray_frame_name = "grayscale_camera_frame_{}.png".format(frame_index)
cv2.imwrite(frame_name, frame)
cv2.imwrite(gray_frame_name, gray_frame)
frame_index += 1

ord('c') returns the value representing the c character using eight bits. Additionally, the cv2.waitKey() value is bitwise AND using the & operator with 0xFF to get only its last eight bits. Therefore, we can perform a comparison between these two 8-bit values. When the C key is pressed, we build the names for both frames. Then, we save the two images to disk. Finally, frame_index is incremented so that it's ready for the next frame to be saved. Check out read_camera_capture.py to see the full code of this script.

主站蜘蛛池模板: 大厂| 云阳县| 忻州市| 西充县| 马关县| 望都县| 贵溪市| 新蔡县| 武功县| 措美县| 波密县| 通道| 新和县| 延庆县| 河北区| 铜川市| 鄯善县| 双柏县| 湟源县| 松江区| 巴林右旗| 汾阳市| 新疆| 申扎县| 玛沁县| 剑阁县| 马鞍山市| 维西| 许昌县| 翼城县| 潮安县| 大同县| 惠来县| 木里| 石河子市| 泸西县| 新民市| 湘潭市| 离岛区| 曲阜市| 紫云|