Photo recognize faces

Requests Import 
Import base64
Import json
Import CV2
# request URL
request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"
# because the picture is a binary stream, it needs to be converted to base64 format
with open ( '1.jpg', 'RB') AS F:
base64_data = base64.b64encode (reached, f.read ())
image64data base64_data.decode = ()
# POST request parameters need to pass
params = { 'image': image64data , 'image_type ':' BASE64 ',' face_field ':' landmark150 '}
# url token values required in
the access_token .98a7279f2b40afdca84975c2132b61ff.2592000.1565942701.282335-16822507 = '24'
# url splice
request_url = request_url + "? = the access_token" + the access_token
headers = { 'Content-Type': 'application/json'}
= requests.post Response (URL = request_url, headers = headers, the params = Data)
IF Response:
# json because a string of the format to be converted, so the direct use response.json () method of
data = response.json ()
# value
Result = data [ 'Result']
# is determined there is a request to the data, and the number of faces. 1
IF and Result Result [ 'face_num'] ==. 1:
LOCATION = Result [ 'face_list'] [0] [ ' LOCATION ']
# use vc2 module, the module is vc2 picture editing tools
Image = cv2.imread (' 1.jpg ')
# is required because the incoming parameters of type int, all strong into int
left = int (LOCATION [ 'left'])
Top = int (LOCATION [ 'Top'])
width = int (LOCATION [ 'width'])
height = int (LOCATION [ 'height'])
# Cv2 using face rectangle drawing
# parameters: the face data, (the left face position, face the top position) (the right face position, face down position) (color values)
cv2.rectangle (Image, ( left, Top), (left + width, Top + height), (0,0,255), 1)
# will draw a good picture into the folder
cv2.imwrite ( '2.jpg', Image)
# draw a face 150 points
landmark150 Result = [ 'face_list'] [0] [ 'landmark150']
points_list = []
Print (landmark150)
for I in landmark150:
Print (I)
points_list.append (landmark150 [I])
for in points_list point:
# parameters: image, center coordinates, the radius of the point size, color, line thickness,
X = int (point [ 'X'])
Y = int (point [ 'Y'])
cv2.circle (Image, (X, Y),. 1, (0,255,1),. 3)
cv2.imwrite ( 'face150.jpg', Image) stored image #

Guess you like

Origin www.cnblogs.com/xiaoxiaoxl/p/11203278.html