The previous several blogs share constructs the face recognition and the sentiment judgment environment and the source code, but does not have the UI, the interface is very difficult to see, one opens is OpenCV pops up a screen box. Virgo I looked very uncomfortable, so decided to make a UI, a little more good-looking, and then how to say, this is a small software, is no longer a running source.
After a round of surfing the internet, it was found to be a vacancy, as if no one was doing this, and the only thing seen was a bit similar to making a video player with Wxpython. And this display OPENCV real-time video screen is still a bit of a gap, but also has a guiding role.
Usage version: python-3.6.3 (Anaconda) opencv-3.4.1 wxpython-4.0.1
Run the process:
1. When running the program, show the cover page first
2, the user click "Start" after the start OpenCV read the video screen, Dlib began processing, and emotional judgment
3, the user click "Close", the end of the video screen, back to the cover page. Wait for another click to start
First, instantiate a frame, add a control
def __init__(self,parent,title): WX. Frame.__init__(Self,parent,title=title,size= (600,600)) Self.panel=WX. Panel (self) self. Center ()#cover PictureSelf.image_cover = wx. Image (COVER, WX. Bitmap_type_any). Scale (350,300) #Show picture on panelSelf.bmp = wx. Staticbitmap (Self.panel, 1, WX. Bitmap (self.image_cover)) Start_button= WX. Button (self.panel,label='Start') Close_button= WX. Button (self.panel,label='Close') self. Bind (WX. Evt_button,self.learning_face,start_button) self. Bind (WX. Evt_button,self.close_face,close_button)
This initialization program adds an image, two buttons, and binds the buttons to their respective action functions. WX is used here. The image method reads a photo in any format, gives it to Staticbitmap, and starts displaying the picture on the panel.
Second, the interface layout based on Gridbagsizer
There are many ways to use the interface layout, which uses Gridbagsizer for interface layout.
It abstracts a panel into a grid, and we can imagine what it looks like in Excel, and the size of the lattice is scaled down according to its own settings. But the total area is the same. # Gridbagsizer-based interface layout
#instance an object firstSelf.grid_bag_sizer = wx. Gridbagsizer (hgap=5,vgap=5) #Note that the POS is the first ordinate after the horizontal axisSelf.grid_bag_sizer. ADD (self.bmp, pos= (0, 0), flag=wx. All | Wx. EXPAND, Span= (4, 4), border=5) Self.grid_bag_sizer. ADD (Start_button, POS= (4, 1), flag=wx. All | Wx. Align_center_vertical, span= (1, 1), border=5) Self.grid_bag_sizer. ADD (Close_button, POS= (4, 2), flag=wx. All | Wx. Align_center_vertical, span= (1, 1), border=5) Self.grid_bag_sizer. Addgrowablecol (0,1)Self.grid_bag_sizer. Addgrowablerow (0,1)
Self.panel.SetSizer (Self.grid_bag_sizer)#interface automatically adjusts the window to fit the contentSelf.grid_bag_sizer. Fit (self)
Use the Add method to add controls to the grid, POS is coordinates, span is the number of rows that the control needs to span, and these two parameters basically determine the position and size of a control.
The following fit method is to make sure that the control is moved proportionally to the size of the panel as it is dragged and dragged.
Three, button action
Now that the control's position is neatly formatted, the following is their action binding.
Click Start, will start OpenCV read the video screen, and then give each frame of the picture to Dlib for face recognition and feature point calibration, and then in the display.
The original is called OpenCV Imshow method to display, the specific sentence is: cv2.imshow ("Camera", im_rd), then we want to show this frame in the framework of wxpython what to do?
Just replace the cover page with OpenCV every frame.
Cv2.imshow ("Camera", IM_RD), is a circular display of its every frame picture, just changed a method, in fact, the principle is the same. The loop displays each frame picture, becomes the video screen;
So in the final analysis, the problem is to show a picture.
There seems to be something wrong. We show that the image format is a JPG image, then what is the format of this im_rd?
Remember this section of code:
# cap.read () # returns a value of two: # A Boolean value true/false to determine if the video read is successful/not to the end of the video # Image object, three-dimensional matrix of the image flag, im_rd = Self.cap.read ()
He is a three-dimensional matrix.
At this point, we need to use the following method to convert the data of this frame, display,
# now convert the OpenCV captured picture BGR to RGB, and then display the picture in the frame of the UI Height,width = Im_rd.shape[:2] = cv2.cvtcolor (im_rd, Cv2. COLOR_BGR2RGB) = wx. Bitmap.frombuffer (width,height,image1) # show pictures on panel Self.bmp.SetBitmap (pic) Self.grid_bag_sizer. Fit (self)
This allows each frame of the video to be displayed on top of the Wxpython panel.
Four, simple multithreading
I wanted to drag this frame when I was able to click Start to display the video screen inside the frame, but the program was stuck! No response??? What??
Check the data to know that our program only one thread, then the program is in the while dead loop inside the video display, occupy the only one thread, if we do UI operation, the program will be crashed.
The solution is to create a new thread that allows the action function of the button to be executed in this new sub-thread, and the main thread we do the UI, such as frame dragging, zooming in and out.
def Learning_face (self,event): """ using multi-threaded, sub-threading programs that run in the background, the main thread updates the foreground UI so that it does not affect each other """ Import _thread # Create a child thread, the button calls this method, _thread.start_new_thread (Self._learning_face, (event,))
At this point, when we click the button start, we begin to execute this method, create a child thread first, and then call the previous method in this method. is to further encapsulate the previous method.
This will not be the phenomenon of the death of the card.
Complete program code: HTTPS://GITEE.COM/ANDREW_QIAN/CODES/ACM80FR6EKJWGPZ27BTHD35
OpenCV Video stream Embedding Wxpython frame