【最新】基于OpenCV图像采集的人脸识别网络推流及局域网无线控制系统(将图像在URL地址上输出,可做成网络摄像头,带识别框)

【最新】基于OpenCV图像采集的人脸识别网络推流及局域网无线控制系统(将图像在URL地址上输出,可做成网络摄像头,带识别框)

前文:
https://blog.csdn.net/weixin_53403301/article/details/124041957

资源:
https://download.csdn.net/download/weixin_53403301/85110021

在运行项目前,先在当前目录下新建目录templates
在目录内新建index.html文件 并输入HTML代码:

<html>
  <head>
    <title>局域网视频推流及控制系统title>
  head>
  <body>
    <h1>局域网视频推流及控制系统h1>    
    <form action="/" method="post">
        <p>
        <input type="submit" style="width:160" name="change" value="切换">
        <input type="submit" style="width:160" name="reset" value="复位">
        <input type="submit" style="width:160" name="set" value="标定">
        <input type="submit" style="width:160" name="quit" value="退出">
        p>
        <p>
        <input type="submit" style="width:160" name="left" value="左移">
        <input type="submit" style="width:160" name="right" value="右移">
        <input type="submit" style="width:160" name="stop" value="停止">
        p>
    form>
    <img src="{{ url_for('video_feed') }}" height="480">
  body>
html>

网页效果:
【最新】基于OpenCV图像采集的人脸识别网络推流及局域网无线控制系统(将图像在URL地址上输出,可做成网络摄像头,带识别框)_第1张图片
首先建立服务器,同时用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 tkinter as tk

local_ip = str(socket.gethostbyname(socket.gethostname()))
local_post = 1212

app = Flask(__name__)

cap = cv2.VideoCapture(0)  # 开启摄像头
classifier = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')



@app.route('/', methods=['GET', 'POST'])
def index():
    global command_str
    command_str = None
    if request.method == 'POST':
        c0 = str(request.form.get('change'))
        c1 = str(request.form.get('quit'))
        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'))
        print(c0,c1,c2,c3,c4,c5,c6)
        for i in [c0,c1,c2,c3,c4,c5,c6]:
            if i != "None":
                command_str = i
                break
        print(command_str)
    return render_template('index.html')

def track():
    global faceImg
    global q
    global command_str
    command_str = None
    q = 0
    while True:
        ok, faceImg = cap.read()  # 读取摄像头图像
        faceImg = cv2.flip(faceImg,1)
        if ok is False:
            print('无法读取到摄像头!')
            break
        gray = cv2.cvtColor(faceImg,cv2.COLOR_BGR2GRAY)
        faceRects = classifier.detectMultiScale(gray,scaleFactor=1.2,minNeighbors=3,minSize=(32, 32))
        if len(faceRects):
            for faceRect in faceRects:
                x,y,w,h = faceRect
                # 框选出人脸   最后一个参数2是框线宽度
                cv2.rectangle(faceImg,(x, y), (x + w, y + h), (0,255,0), 2)           


        cv2.imshow("http://"+local_ip+":"+str(local_post)+"/ (img: video_feed)",faceImg)
        # 展示图像
        if command_str == "复位":
            print("Reset")
        elif command_str == "标定":
            print("Set")
        elif command_str == "切换":
            print("Change")
        elif command_str == "左移":
            print("Left")
        elif command_str == "右移":
            print("Right")
        elif command_str == "停止":
            print("Stop")
        else:
            command_str = command_str
        
        if cv2.waitKey(10) == 27 or command_str == "退出":   # 通过esc键退出摄像
            command_str = None
            print("Quit")
            break
        command_str = None
    cap.release()
    cv2.destroyAllWindows()
    q = 1
        
def send_img():
    global faceImg
    global q
    q = 0
    while True:
        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 central_win(win):
#    win.resizable(0,0)                      # 不可缩放
#    screenwidth = win.winfo_screenwidth()	# 获取屏幕分辨率宽
#    screenheight = win.winfo_screenheight()	# 获取屏幕分辨率高
#    win.update()	# 更新窗口
#    width = win.winfo_width()	# 重新赋值
#    height = win.winfo_height()
#    size = '+%d+%d' % ((screenwidth - width)/2, (screenheight - height)/2)
#    # 重新赋值大小 大小为屏幕大小/2
#    win.geometry(size) 	# 以新大小定义窗口
#    
#def info_win(str_list):
#    info=tk.Tk()
#    info.title("小提示")
#    info.attributes('-topmost',1)
#    infofram=tk.Frame(info,width=320, height=260)
#    infofram.grid_propagate(0)
#    infofram.grid()
#    central_win(info)
#    
#    labelName=tk.Label(info, text=str_list, justify=tk.LEFT)
#    labelName.place(x=10, y=20, width=300, height=160)
#
#    def go_info():
#        info.destroy()        
#
#    tk.Button(infofram,width=30,text="确定",command=go_info).place(x=10, y=200, width=300, height=40)
#
#    info.mainloop()
#    return 
    
#def ip_win():
#    info_win("本机IP地址为:"+local_ip)
    
#thread_win = threading.Thread(target=ip_win)
#thread_win.setDaemon(True)
#thread_win.start()

thread_img = threading.Thread(target=track)
thread_img.setDaemon(True)
thread_img.start()

thread_send = threading.Thread(target=start_server)
thread_send.setDaemon(True)
thread_send.start()

效果如下:
【最新】基于OpenCV图像采集的人脸识别网络推流及局域网无线控制系统(将图像在URL地址上输出,可做成网络摄像头,带识别框)_第2张图片
手机端效果:
【最新】基于OpenCV图像采集的人脸识别网络推流及局域网无线控制系统(将图像在URL地址上输出,可做成网络摄像头,带识别框)_第3张图片

你可能感兴趣的:(Python,OpenCV,计算机视觉,服务器,HTML)