前文:
https://blog.csdn.net/weixin_53403301/article/details/124041957
资源:
https://download.csdn.net/download/weixin_53403301/85110021
在运行项目前,先在当前目录下新建目录templates
在目录内新建index.html文件 并输入HTML代码:
<html>
<!--meta http-equiv="refresh" content="5"-->
<head>
<title>智能导播系统</title>
</head>
<body>
<h1>智能导播系统</h1>
<form action="/" method="post" style="float:left">
<p>
<input type="submit" style="font-size:50px" name="auto" value="自动模式">
<input type="submit" style="font-size:50px" name="manu" value="手动模式">
</p>
<p>
<input type="submit" style="font-size:50px" name="reset" value="复位导轨">
<input type="submit" style="font-size:50px" name="set" value="设置点位">
</p>
<p>
<input type="submit" style="font-size:50px" name="left" value="左移导轨">
<input type="submit" style="font-size:50px" name="right" value="右移导轨">
</p>
<p>
<input type="submit" style="font-size:50px" name="go_in" value="继续程序">
<input type="submit" style="font-size:50px" name="go_out" value="暂停程序">
</p>
<p>
<input type="submit" style="font-size:50px" name="stop" value="停止导轨">
<input type="submit" style="font-size:50px" name="quit" value="全部退出">
</p>
<p>
<input type="submit" style="font-size:50px" name="left_step" value="左移一步">
<input type="submit" style="font-size:50px" name="right_step" value="右移一步">
</p>
</form>
<img src="{{ url_for('video_feed') }}" height="540" style="float:left">
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<h2>手势及功能说明</h2>
<h4 style="color:red">起始手势——左/右手“5”:开启手势控制模式,其他手势控制功能仅在开启该模式时有效,若10秒内无任一手势控制操作,则需手势“5"解锁;五指全部打开。</h4>
<h4 style="color:blue">自动模式——右手“Spider-Man”:该模式下自动识别人脸并追踪,手动模式操作不可用;打开拇指、食指、小指,闭合中指、无名指。</h4>
<h4 style="color:green">手动模式——左手“Rock'n'Roll”:该模式下不进行识别人脸或追踪,手动模式操作可用;打开食指、小指,闭合拇指、中指、无名指。</h4>
<h4 style="color:blue">复位导轨——右手“Thumb Up”:手动模式操作。使导轨复位到标记的初始位置;竖起拇指,其他闭合。</h4>
<h4 style="color:blue">设置点位——右手“Princess”:手动模式操作。标记导轨初始位置;打开中指、无名指、小指,闭合拇指、食指,又为“OK”手势。</h4>
<h4 style="color:green">左移导轨——左手“2”:手动模式操作。左移导轨;竖起食指、中指,其他闭合。</h4>
<h4 style="color:green">右移导轨——左手“1”:手动模式操作。右移导轨;竖起食指,其他闭合。</h4>
<h4 style="color:blue">继续程序——右手“8”:恢复暂停的程序及功能;竖起拇指、食指,其他闭合。</h4>
<h4 style="color:blue">暂停程序——右手“6”:暂停图像、识别、运动、手势功能,仅保留网页端控制功能;竖起拇指、小指,其他闭合。</h4>
<h4 style="color:green">停止导轨——左手“0”:手动模式操作。停止导轨;全部闭合,握拳。</h4>
<h4 style="color:red">全部退出——左/右手“Bye”:保持三秒即可退出所有程序,退出后无法执行任何功能;竖起拇指、食指、中指,其他闭合。</h4>
<h4 style="color:green">左移一步——左手“4”:手动模式操作。左移一步;拇指闭合,其他打开。</h4>
<h4 style="color:green">右移一步——左手“3”:手动模式操作。右移一步;拇指、小指闭合,其他打开。</h4>
</body>
</html>
网页效果:
首先建立服务器,同时用OpenCV调用本地摄像头采集图像 并转为JPEG格式 而后进行推流
同时监控表单输入,使其能够执行其他函数
服务器建立成功后,即可在浏览器输入IP地址和端口进行查看
在浏览器中输入http://A.B.C.D:yyyy/打开即可
其中A.B.C.D为服务器IP地址 yyyy为端口号 这里设置的为1212
代码如下:
import cv2
from flask import Flask, render_template, Response, request
import threading
import socket
import mediapipe as mp
import math
import time
global q
q = 0
global kill_all_flag
kill_all_flag = 0
global model_flag
model_flag = 0
global command_str
command_str = None
local_post = 1212
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.connect(("8.8.8.8",80))
local_ip = str(s.getsockname()[0])
s.close()
app = Flask(__name__)
cap = cv2.VideoCapture(0) # 开启摄像头
classifier = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')
global cam_img
global faceImg
ok, cam_img = cap.read() # 读取摄像头图像
if ok is False:
q = 1
kill_all_flag = 1
print('无法读取到摄像头!')
faceImg = cam_img
high=faceImg.shape[0]
width=faceImg.shape[1]
left_point = width/2+25
right_point = width/2-25
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(static_image_mode=False,max_num_hands=1,min_detection_confidence=0.75,min_tracking_confidence=0.75)
def vector_2d_angle(v1,v2):
'''
求解二维向量的角度
'''
v1_x=v1[0]
v1_y=v1[1]
v2_x=v2[0]
v2_y=v2[1]
try:
angle_= math.degrees(math.acos((v1_x*v2_x+v1_y*v2_y)/(((v1_x**2+v1_y**2)**0.5)*((v2_x**2+v2_y**2)**0.5))))
except:
angle_ =65535.
if angle_ > 180.:
angle_ = 65535.
return angle_
def hand_angle(hand_):
'''
获取对应手相关向量的二维角度,根据角度确定手势
'''
angle_list = []
#---------------------------- thumb 大拇指角度
angle_ = vector_2d_angle(
((int(hand_[0][0])- int(hand_[2][0])),(int(hand_[0][1])-int(hand_[2][1]))),
((int(hand_[3][0])- int(hand_[4][0])),(int(hand_[3][1])- int(hand_[4][1])))
)
angle_list.append(angle_)
#---------------------------- index 食指角度
angle_ = vector_2d_angle(
((int(hand_[0][0])-int(hand_[6][0])),(int(hand_[0][1])- int(hand_[6][1]))),
((int(hand_[7][0])- int(hand_[8][0])),(int(hand_[7][1])- int(hand_[8][1])))
)
angle_list.append(angle_)
#---------------------------- middle 中指角度
angle_ = vector_2d_angle(
((int(hand_[0][0])- int(hand_[10][0])),(int(hand_[0][1])- int(hand_[10][1]))),
((int(hand_[11][0])- int(hand_[12][0])),(int(hand_[11][1])- int(hand_[12][1])))
)
angle_list.append(angle_)
#---------------------------- ring 无名指角度
angle_ = vector_2d_angle(
((int(hand_[0][0])- int(hand_[14][0])),(int(hand_[0][1])- int(hand_[14][1]))),
((int(hand_[15][0])- int(hand_[16][0])),(int(hand_[15][1])- int(hand_[16][1])))
)
angle_list.append(angle_)
#---------------------------- pink 小拇指角度
angle_ = vector_2d_angle(
((int(hand_[0][0])- int(hand_[18][0])),(int(hand_[0][1])- int(hand_[18][1]))),
((int(hand_[19][0])- int(hand_[20][0])),(int(hand_[19][1])- int(hand_[20][1])))
)
angle_list.append(angle_)
return angle_list
def h_gesture(angle_list):
'''
# 二维约束的方法定义手势
# fist five gun love one six three thumbup yeah
'''
thr_angle = 65. #手指闭合则大于这个值(大拇指除外)
thr_angle_thumb = 53. #大拇指闭合则大于这个值
thr_angle_s = 49. #手指张开则小于这个值
gesture_str = "Unknown"
if 65535. not in angle_list:
if (angle_list[0]>thr_angle_thumb) and (angle_list[1]>thr_angle) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]>thr_angle):
gesture_str = "0"
elif (angle_list[0]>thr_angle_thumb) and (angle_list[1]<thr_angle_s) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]>thr_angle):
gesture_str = "1"
elif (angle_list[0]>thr_angle_thumb) and (angle_list[1]<thr_angle_s) and (angle_list[2]<thr_angle_s) and (angle_list[3]>thr_angle) and (angle_list[4]>thr_angle):
gesture_str = "2"
elif (angle_list[0]>thr_angle_thumb) and (angle_list[1]<thr_angle_s) and (angle_list[2]<thr_angle_s) and (angle_list[3]<thr_angle_s) and (angle_list[4]>thr_angle):
gesture_str = "3"
elif (angle_list[0]>thr_angle_thumb) and (angle_list[1]<thr_angle_s) and (angle_list[2]<thr_angle_s) and (angle_list[3]<thr_angle_s) and (angle_list[4]<thr_angle_s):
gesture_str = "4"
elif (angle_list[0]<thr_angle_s) and (angle_list[1]<thr_angle_s) and (angle_list[2]<thr_angle_s) and (angle_list[3]<thr_angle_s) and (angle_list[4]<thr_angle_s):
gesture_str = "5"
elif (angle_list[0]<thr_angle_s) and (angle_list[1]>thr_angle) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]<thr_angle_s):
gesture_str = "6"
elif (angle_list[0]<thr_angle_s) and (angle_list[1]<thr_angle_s) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]>thr_angle):
gesture_str = "8"
elif (angle_list[0]>thr_angle_thumb) and (angle_list[1]>thr_angle) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]<thr_angle_s):
gesture_str = "Pink Up"
elif (angle_list[0]<thr_angle_s) and (angle_list[1]>thr_angle) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]>thr_angle):
gesture_str = "Thumb Up"
elif (angle_list[0]>thr_angle_thumb) and (angle_list[1]>thr_angle) and (angle_list[2]<thr_angle_s) and (angle_list[3]>thr_angle) and (angle_list[4]>thr_angle):
gesture_str = "Fuck"
elif (angle_list[0]>thr_angle_thumb) and (angle_list[1]>thr_angle) and (angle_list[2]<thr_angle_s) and (angle_list[3]<thr_angle_s) and (angle_list[4]<thr_angle_s):
gesture_str = "Princess"
elif (angle_list[0]<thr_angle_s) and (angle_list[1]<thr_angle_s) and (angle_list[2]<thr_angle_s) and (angle_list[3]>thr_angle) and (angle_list[4]>thr_angle):
gesture_str = "Bye"
elif (angle_list[0]<thr_angle_s) and (angle_list[1]<thr_angle_s) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]<thr_angle_s):
gesture_str = "Spider-Man"
elif (angle_list[0]>thr_angle_thumb) and (angle_list[1]<thr_angle_s) and (angle_list[2]>thr_angle) and (angle_list[3]>thr_angle) and (angle_list[4]<thr_angle_s):
gesture_str = "Rock'n'Roll"
return gesture_str
def command_jugg():
global kill_all_flag
global q
global command_list
global model_flag
for i in command_list:
if i != "None":
command_str = i
break
print(command_str)
if command_str == "暂停程序":
q = 1
print("Pause")
elif command_str == "继续程序":
q = 0
print("Continue")
elif command_str == "全部退出":
kill_all_flag = 1
q = 1
print("Good-Bye")
elif command_str == "自动模式":
model_flag = 1
print("Automatic")
elif command_str == "手动模式":
model_flag = 0
print("Manual")
if model_flag == 0:
if command_str == "复位导轨":
print("Reset")
elif command_str == "设置点位":
print("Set")
elif command_str == "左移导轨":
print("Left")
elif command_str == "右移导轨":
print("Right")
elif command_str == "停止导轨":
print("Stop")
command_str = None
@app.route('/', methods=['GET', 'POST'])
def index():
global command_list
global kill_all_flag
global q
global command_str
if request.method == 'POST':
command_list = []
c0 = str(request.form.get('auto'))
c1 = str(request.form.get('manu'))
c2 = str(request.form.get('left'))
c3 = str(request.form.get('right'))
c4 = str(request.form.get('stop'))
c5 = str(request.form.get('reset'))
c6 = str(request.form.get('set'))
c7 = str(request.form.get('quit'))
c8 = str(request.form.get('go_in'))
c9 = str(request.form.get('go_out'))
command_list = [c0,c1,c2,c3,c4,c5,c6,c7,c8,c9]
if kill_all_flag == 0 and q == 0:
command_jugg()
elif kill_all_flag == 0 and q == 1:
for i in command_list:
if i != "None":
command_str = i
break
print(command_str)
if command_str == "继续程序":
q = 0
print("Continue")
else:
print("程序已暂停,继续请按键")
else:
print("程序已终止,请重启程序")
return render_template('index.html')
def hand_detect():
global faceImg
global q
global kill_all_flag
global cam_img
global hand_jugg_flag
global hand_str_flag
global hand_jugg
global gesture_str
# global hand_landmarks
# global draw_flag
global model_flag
detect_flag = 0
detect_time = time.time()
bye_flag = 0
bye_time = time.time()
hand_jugg = None
gesture_str = None
while True:
time.sleep(0.1)
while q==0:
time.sleep(0.1)
hand_jugg_flag = 0
hand_str_flag = 0
# draw_flag = 0
frame = cv2.cvtColor(cam_img, cv2.COLOR_BGR2RGB)
results = hands.process(frame)
if results.multi_handedness:
for hand_label in results.multi_handedness:
hand_jugg=str(hand_label).split('"')[1]+" Hand"
print(hand_jugg)
hand_jugg_flag = 1
# cv2.putText(faceImg,hand_jugg,(50,200),0,1.3,(0,0,255),2)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(faceImg, hand_landmarks, mp_hands.HAND_CONNECTIONS)
# draw_flag = 1
hand_local = []
for i in range(21):
x = hand_landmarks.landmark[i].x*frame.shape[1]
y = hand_landmarks.landmark[i].y*frame.shape[0]
hand_local.append((x,y))
if hand_local:
angle_list = hand_angle(hand_local)
gesture_str = h_gesture(angle_list)
print(gesture_str)
hand_str_flag = 1
# cv2.putText(faceImg,gesture_str,(50,100),0,1.3,(0,0,255),2)
if gesture_str == "5":
bye_flag = 0
detect_flag = 1
detect_time = time.time()
gesture_str = None
if detect_flag == 1:
if gesture_str == "Bye":
detect_time = time.time()
if bye_flag == 0:
bye_time = time.time()
bye_flag = 1
elif bye_flag == 1 and time.time() - bye_time >= 3:
kill_all_flag = 1
q = 1
print("Good-Bye")
else:
bye_flag = 1
elif hand_jugg == "Right Hand" and gesture_str == "Spider-Man":
detect_time = time.time()
bye_flag = 0
model_flag = 1
print("Automatic")
elif hand_jugg == "Left Hand" and gesture_str == "Rock'n'Roll":
detect_time = time.time()
bye_flag = 0
model_flag = 0
print("Manual")
elif hand_jugg == "Right Hand" and gesture_str == "6":
detect_time = time.time()
bye_flag = 0
q = 1
print("Pause")
elif hand_jugg == "Right Hand" and gesture_str == "8":
detect_time = time.time()
bye_flag = 0
q = 0
print("Continue")
else:
bye_flag = 0
if model_flag == 0:
if gesture_str == "Bye":
detect_time = time.time()
if bye_flag == 0:
bye_time = time.time()
bye_flag = 1
elif bye_flag == 1 and time.time() - bye_time >= 3:
kill_all_flag = 1
q = 1
print("Good-Bye")
else:
bye_flag = 1
elif hand_jugg == "Left Hand" and gesture_str == "1":
detect_time = time.time()
bye_flag = 0
print("Right")
elif hand_jugg == "Left Hand" and gesture_str == "2":
detect_time = time.time()
bye_flag = 0
print("Left")
elif hand_jugg == "Left Hand" and gesture_str == "0":
detect_time = time.time()
bye_flag = 0
print("Central")
elif hand_jugg == "Right Hand" and gesture_str == "Thumb Up":
detect_time = time.time()
bye_flag = 0
print("Reset")
elif hand_jugg == "Right Hand" and gesture_str == "Princess":
detect_time = time.time()
bye_flag = 0
print("Set")
else:
bye_flag = 0
hand_jugg = None
gesture_str = None
if time.time() - detect_time >= 10:
detect_flag = 0
if q == 1:
# STOP()
print("Stop")
break
if kill_all_flag == 1:
break
# STOP()
print("Stop")
return
def track():
global faceImg
global q
global kill_all_flag
global cam_img
global x,y,w,h
global track_flag
global model_flag
while True:
time.sleep(0.1)
while q==0:
time.sleep(0.1)
track_flag = 0
if model_flag == 1:
gray = cv2.cvtColor(cam_img,cv2.COLOR_BGR2GRAY)
faceRects = classifier.detectMultiScale(gray,scaleFactor=1.2,minNeighbors=3,minSize=(32, 32))
if len(faceRects):
track_flag = 1
x,y,w,h = faceRects[0]
# 框选出人脸 最后一个参数2是框线宽度
# cv2.rectangle(faceImg,(x, y), (x + w, y + h), (0,255,0), 2)
central_point = x+w/2
if central_point > left_point:
# LEFT()
print("Right")
elif central_point < right_point:
# RIGHT()
print("Left")
else:
# STOP()
print("Central")
if q == 1:
# STOP()
print("Stop")
break
if kill_all_flag == 1:
break
# STOP()
print("Stop")
return
def draw():
global faceImg
global q
global kill_all_flag
# global cam_img
global x,y,w,h
global track_flag
global hand_jugg_flag
global hand_str_flag
global hand_jugg
global gesture_str
# global hand_landmarks
# global draw_flag
while True:
time.sleep(0.1)
while q==0:
time.sleep(0.1)
# faceImg = cam_img
# if draw_flag == 1:
# mp_drawing.draw_landmarks(faceImg, hand_landmarks, mp_hands.HAND_CONNECTIONS)
if track_flag == 1:
cv2.rectangle(faceImg,(x, y), (x + w, y + h), (0,255,0), 2)
if hand_str_flag == 1:
cv2.putText(faceImg,gesture_str,(50,100),0,1.3,(0,0,255),2)
if hand_jugg_flag == 1:
cv2.putText(faceImg,hand_jugg,(50,200),0,1.3,(0,0,255),2)
if q == 1: # 通过esc键退出摄像
q = 1
# STOP()
print("Stop")
break
if kill_all_flag == 1:
break
def img_main():
global faceImg
global q
global kill_all_flag
global cam_img
global x,y,w,h
global track_flag
global hand_jugg_flag
global hand_str_flag
global hand_jugg
global gesture_str
# global hand_landmarks
# global draw_flag
hand_jugg = None
x,y,w,h = None,None,None,None
gesture_str = None
track_flag = 0
hand_str_flag = 0
hand_jugg_flag = 0
thread_track = threading.Thread(target=track)
thread_track.setDaemon(True)
thread_track.start()
thread_hand = threading.Thread(target=hand_detect)
thread_hand.setDaemon(True)
thread_hand.start()
# thread_draw = threading.Thread(target=draw)
# thread_draw.setDaemon(True)
# thread_draw.start()
while True:
time.sleep(0.1)
while q==0:
cam_img = cv2.flip(cap.read()[1],1)
faceImg = cam_img
cv2.imshow("http://"+local_ip+":"+str(local_post)+"/ (img: video_feed)",faceImg)
# 展示图像
if q == 1: # 通过esc键退出摄像
q = 1
# STOP()
print("Stop")
print("暂停程序")
cv2.destroyAllWindows()
break
if cv2.waitKey(10) == 27:
kill_all_flag = 1
q = 1
print("Stop")
print("结束程序")
cv2.destroyAllWindows()
break
if kill_all_flag == 1:
break
cap.release()
# STOP()
print("Stop")
print("全部退出")
return
def send_img():
global faceImg
global q
while q == 0:
image = cv2.imencode('.jpg', faceImg)[1].tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + image + b'\r\n')
if q == 1:
break
return
@app.route('/video_feed')
def video_feed():
return Response(send_img(), mimetype='multipart/x-mixed-replace; boundary=frame')
def start_server():
app.run(host='0.0.0.0', port=local_post)
def main():
thread_send = threading.Thread(target=start_server)
thread_send.setDaemon(True)
thread_send.start()
img_main()
time.sleep(1)
print("Stop")
print("已退出所有程序")
return
if __name__ == "__main__":
main()
效果如下:
手机端效果: