#!/usr/bin/python3 # ==================================================================== # draw a rectangle around each face found in a image # ==================================================================== import face_recognition import graphics as gr VERBOSE = False # -------------------------------------------------------------------- # ---- create a graphics window with an image as background # -------------------------------------------------------------------- def create_graphics_window(image,width,height,title): win = gr.GraphWin(title,width,height) i = gr.Image(gr.Point(round(width/2),round(height/2)),image) i.draw(win) return win # -------------------------------------------------------------------- # ---- draw line # -------------------------------------------------------------------- def draw_line(win,x1,y1,x2,y2,fill='black',width=2): l = gr.Line(gr.Point(x1,y1),gr.Point(x2,y2)) l.setWidth(width) if fill is not None: l.setFill(fill) l.draw(win) return l # -------------------------------------------------------------------- # ---- draw rectangle # -------------------------------------------------------------------- def draw_rectangle(win,x1,y1,x2,y2,outline='black',fill=None,width=2): r = gr.Rectangle(gr.Point(x1,y1),gr.Point(x2,y2)) r.setOutline(outline) r.setWidth(width) if fill is not None: r.setFill(fill) r.draw(win) return r # -------------------------------------------------------------------- # ---- draw circle # -------------------------------------------------------------------- def draw_circle(win,x,y,raidus,outline='black',fill=None,width=2): c = gr.Circle(gr.Point(x,y),raidus) c.setWidth(width) c.setOutline(outline) if fill is not None: c.setFill(fill) c.setOutline('black') c.draw(win) return c # -------------------------------------------------------------------- # ---- draw a rectangle around each face found in the image # -------------------------------------------------------------------- def draw_face_rectangles(win,locations): global VERBOSE i = 0 # drawn rectangle count for loc in locations: i += 1 # ---- convert image coordinates to window coordinates # ---- face location is (top,right,bottom,left) wx1 = loc[3] # left corner x wy1 = loc[2] # top corner y wx2 = loc[1] # right corner x wy2 = loc[0] # bottom corner y if VERBOSE: ##print(f'face {i}: pic_width={win.width} pic_height={win.height}') print(f'face {i}: ({wx1},{wy1}) ({wx2},{wy2})') # ---- draw a rectangle around a face draw_rectangle(win,wx1,wy1,wx2,wy2) return i # -------------------------------------------------------------------- # ---- main # -------------------------------------------------------------------- image01 = ('./faces/ch_01.png',600,450) # name,width,height image02 = ('./faces/tj_01.png',623,562) # name,width,height image03 = ('./faces/tj_02.png',403,600) # name,width,height image04 = ('./faces/crowd_01.png',800,600) # name,width,height pic = image04 # ---- create a graphics window the size of the image win = create_graphics_window(pic[0],pic[1],pic[2],'Faces') # ---- get the location of faces in the image image = face_recognition.load_image_file(pic[0]) locations = face_recognition.face_locations(image) if VERBOSE: print(f'found {len(locations)} faces in image') # ---- draw a rectangle around each face found in the image draw_face_rectangles(win,locations) # ---- to exit program, click mouse in window win.getMouse() win.close()