版权声明:添加我的微信wlagooble,开启一段不一样的旅程 https://blog.csdn.net/nineship/article/details/84204972
#-*-coding:utf-8-*-
import requests
from json import JSONDecoder
import cv2
import time
import threading
import matplotlib.pyplot as plt
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import math
from PIL import Image
rotate = 0
r_l = 0
capture = cv2.VideoCapture(0)
def read_cam():
global rotate,r_l,r_n
ret, frame = capture.read()
cv2.imwrite("1.jpg",frame)
face_img = open("1.jpg","rb")
key,box = compareIm(face_img)
if key == None:
return
show_person(key)
left_eye_bottom, right_eye_bottom = key['left_eye_bottom'],key['right_eye_bottom']
rotate = math.atan((left_eye_bottom['y']-right_eye_bottom['y'])/(left_eye_bottom['x']-right_eye_bottom['x']))
def drawFunc():
global rotate,r_l
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
#沿z轴平移
glTranslate(0.0, 0.0, -3.0)
glBindTexture(GL_TEXTURE_2D, 0)
#绘制四边形
glBegin(GL_QUADS)
#设置纹理坐标
glTexCoord2f(0.0, 0.0)
#绘制顶点
glVertex3f(-1.0, -1.0, 1.0)
glTexCoord2f(1.0, 0.0)
glVertex3f(1.0, -1.0, 1.0)
glTexCoord2f(1.0, 1.0)
glVertex3f(1.0, 1.0, 1.0)
glTexCoord2f(0.0, 1.0)
glVertex3f(-1.0, 1.0, 1.0)
glEnd()
print ("r",rotate)
print ("-",rotate-r_l)
#glLoadIdentity()
glRotatef(-rotate*50,0,0,1)
glutSolidTeapot(1)
glFlush()
glutSwapBuffers()
r_l = rotate
def compareIm(face_img):
try:
#官方给你的接口地址
compare_url = "https://api-cn.faceplusplus.com/facepp/v3/detect"
#创建应用分配的key和secret
key = "A3RnAzoqyAetktbsK0xyGU2DYUuP9IqC"
secret = "OLA9nb6YDQcZ_dcu8UBPR2jkwAfGwJEi"
#创建请求数
data = {"api_key": key, "api_secret": secret,"return_landmark":1}
files = {"image_file": face_img}
#通过接口发送请求
response = requests.post(compare_url, data=data, files=files)
req_con = response.content.decode('utf-8')
req_dict = JSONDecoder().decode(req_con)
return req_dict['faces'][0]['landmark'],req_dict['faces'][0]['face_rectangle']
except Exception:
pass
print("无法识别!")
return None,None
def draw_pot(x,y):
#plt.plot(x,y)
plt.clf()
plt.ion()
ax = plt.gca()
ax.xaxis.set_ticks_position('top')
ax.invert_yaxis()
plt.scatter(x, y,c='r')
plt.pause(0.01) # 暂停一秒
def show_person(Per_key):
if Per_key == None:
return
X_show = []
Y_show = []
for i in Per_key:
X_show.append(Per_key[i]['x'])
Y_show.append(Per_key[i]['y'])
draw_pot(X_show,Y_show)
def timerProc(id1):
read_cam()
LoadTexture()
drawFunc()
glutTimerFunc(2,timerProc,1)
#加载纹理
def LoadTexture():
width=640
height=480
imgFiles = ['1.jpg']
img = Image.open(imgFiles[0])
width, height = img.size
img = img.tobytes('raw', 'RGBX', 0, -1)
glGenTextures(2)
glBindTexture(GL_TEXTURE_2D, 0)
glTexImage2D(GL_TEXTURE_2D, 0, 4,
width, height, 0, GL_RGBA,
GL_UNSIGNED_BYTE,img)
glTexParameterf(GL_TEXTURE_2D,
GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D,
GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D,
GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D,
GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameterf(GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexEnvf(GL_TEXTURE_ENV,
GL_TEXTURE_ENV_MODE, GL_DECAL)
def InitGL(width, height):
#LoadTexture()
glEnable(GL_TEXTURE_2D)
glClearColor(1.0, 1.0, 1.0, 0.0)
glClearDepth(1.0)
glDepthFunc(GL_LESS)
glShadeModel(GL_SMOOTH)
glEnable(GL_CULL_FACE)
glCullFace(GL_BACK)
glEnable(GL_POINT_SMOOTH)
glEnable(GL_LINE_SMOOTH)
glEnable(GL_POLYGON_SMOOTH)
glMatrixMode(GL_PROJECTION)
glHint(GL_POINT_SMOOTH_HINT,GL_NICEST)
glHint(GL_LINE_SMOOTH_HINT,GL_NICEST)
glHint(GL_POLYGON_SMOOTH_HINT,GL_FASTEST)
glLoadIdentity()
gluPerspective(45.0, float(width)/float(height), 0.1, 100.0)
glMatrixMode(GL_MODELVIEW)
glutInit()
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
glutInitWindowPosition(0,0)
glutInitWindowSize(640,480)
glutCreateWindow(b"first")
glutDisplayFunc(drawFunc)
glutIdleFunc(drawFunc)
glutTimerFunc(10,timerProc,1)
InitGL(640,480)
glutMainLoop()