Python+opencv+tkinter Integrated Demo
Because of the need to use OPENCV for small demo development, but because the OPENCV with the GUI function is still very simple, so use opencvv+tkinter combination implementation.
First, the result chart.
In the GUI click on the point of Praise button in the console can show "Someone to give you a little praise."
Directly on the code:
fromTkinterImport*ImportCv2 fromPiLImportImage,imagetkdefTake_snapshot (): Print ("someone gave you some praise. ")defVideo_loop (): Success, img = Camera.read () # Read photos from the cameraifSuccess:cv2.waitKey (1000) cv2image = Cv2.cvtcolor (img, Cv2. Color_bgr2rgba) #转换颜色从BGR到RGBA current_image = Image.fromarray (cv2image) #将图像转换成Image对象 IMGTK = Imagetk.phot Oimage (image=current_image) panel.imgtk = Imgtk panel.config (IMAGE=IMGTK) root.after (1, Video_loop ) camera = Cv2. Videocapture (0) #摄像头 root = Tk () root.title ("OpenCV + tkinter"#root. Protocol (' Wm_delete_window ', detector) panel = Label (root) # Initialize image panel Panel.pack (padx=10, pady=10) Root.config (cursor="Arrow") btn = Button (Root, text="dot Praise!", Command=take_snapshot) Btn.pack (fill="both", expand=True, padx=10, pady=10) Video_loop () Root.mainloop () # When all is done, turn off the camera and release the occupied Resources Camera.release () cv2.destroyallwindows ()
Detailed comments are not described in detail in the code.
Code can be copied and pasted directly to run locally.