当前位置:   article > 正文

python读取摄像头,做人脸识别,并把视频推送到html页面显示_python将视频框放在网页里

python将视频框放在网页里

本文的重点内容为opencv+flask+face_recognition

导读

先上开源代码,GitHub - Kr1s77/flask-video-streaming-recorder: opencv+flask 家庭监控系统(surveillance_system)  作者通过opencv+flask完成了读取摄像头,并且在HTML页面显示的功能,很多场景都会用到,本文是分享二次开发人脸识别。

环境正常登录

源码的main.py中源码app.run(threaded=True, host="0.0.0.0"),默认了port=5000,端口是可以改变的,如下app.run(threaded=True, host="0.0.0.0",port=5002)

改完之后运行python main.py,打开浏览器访问http://localhost:5002/login 就可以看到登录页面啦!(作者忘了提示这步)

  1. Username: admin
  2. Password: admin

加入人脸识别功能

本文用开源的face_recognition(如果没有安装就pip install face_recognition)

只需要修改一个文件:controller/utils/camera.py,下面直接给完整代码

  1. import cv2
  2. import threading
  3. import face_recognition
  4. class RecordingThread(threading.Thread):
  5. def __init__(self, name, camera):
  6. threading.Thread.__init__(self)
  7. self.name = name
  8. self.isRunning = True
  9. self.cap = camera
  10. fourcc = cv2.VideoWriter_fourcc(*'MJPG')
  11. self.out = cv2.VideoWriter('./static/video.avi', fourcc, 20.0, (640, 480))
  12. def run(self):
  13. while self.isRunning:
  14. ret, frame = self.cap.read()
  15. if ret:
  16. self.out.write(frame)
  17. self.out.release()
  18. def stop(self):
  19. self.isRunning = False
  20. def __del__(self):
  21. self.out.release()
  22. class VideoCamera(object):
  23. def __init__(self):
  24. # 打开摄像头, 0代表笔记本内置摄像头
  25. self.cap = cv2.VideoCapture(0)
  26. #初始化人脸
  27. obama_img = face_recognition.load_image_file("obama.jpg")
  28. self.obama_face_encoding = face_recognition.face_encodings(obama_img)[0]
  29. self.face_locations = []
  30. self.face_encodings = []
  31. self.face_names = []
  32. self.process_this_frame = True
  33. # 初始化视频录制环境
  34. self.is_record = False
  35. self.out = None
  36. # 视频录制线程
  37. self.recordingThread = None
  38. # 退出程序释放摄像头
  39. def __del__(self):
  40. self.cap.release()
  41. def get_frame(self):
  42. ret, frame = self.cap.read()
  43. if ret:
  44. # 将视频帧调整为1/4大小,以加快脸部识别处理
  45. small_frame = cv2.resize(frame,(0,0),fx=0.25, fy=0.25)
  46. # 将图像从BGR颜色(OpenCV使用的)转换为RGB颜色(face_recognition使用)
  47. rgb_small_frame = small_frame[:, :, ::-1]
  48. if self.process_this_frame:
  49. # 查找当前视频帧中的所有面部和脸部编码
  50. self.face_locations = face_recognition.face_locations(small_frame)
  51. self.face_encodings = face_recognition.face_encodings(small_frame, self.face_locations)
  52. self.face_names = []
  53. #处理多张人脸的情况
  54. for self.face_encoding in self.face_encodings:
  55. # 查看脸部是否与已知脸部相匹配(S)
  56. match = face_recognition.compare_faces([self.obama_face_encoding], self.face_encoding)
  57. if match[0]:
  58. name = "obama"
  59. else:
  60. name = "unkonwn"
  61. self.face_names.append(name)
  62. self.process_this_frame = not self.process_this_frame
  63. #对有人脸的图进行处理
  64. for (top, right, bottom, left), name in zip(self.face_locations, self.face_names):
  65. # 自从我们检测到的框架缩放到1/4尺寸后,缩放后面的位置
  66. top *= 4
  67. right *= 4
  68. bottom *= 4
  69. left *= 4
  70. # 在脸上画一个方框
  71. cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
  72. # 在脸部下面画一个名字
  73. cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), 2)
  74. font = cv2.FONT_HERSHEY_DUPLEX
  75. cv2.putText(frame, name, (left+6, bottom-6), font, 1.0, (255, 255, 255), 1)
  76. # 因为opencv读取的图片并非jpeg格式,因此要用motion JPEG模式需要先将图片转码成jpg格式图片
  77. ret, jpeg = cv2.imencode('.jpg', frame)
  78. # 视频录制
  79. if self.is_record:
  80. if self.out == None:
  81. fourcc = cv2.VideoWriter_fourcc(*'MJPG')
  82. self.out = cv2.VideoWriter('./static/video.avi', fourcc, 20.0, (640, 480))
  83. ret, frame = self.cap.read()
  84. if ret:
  85. self.out.write(frame)
  86. else:
  87. if self.out != None:
  88. self.out.release()
  89. self.out = None
  90. return jpeg.tobytes()
  91. else:
  92. return None
  93. def start_record(self):
  94. self.is_record = True
  95. self.recordingThread = RecordingThread("Video Recording Thread", self.cap)
  96. self.recordingThread.start()
  97. def stop_record(self):
  98. self.is_record = False
  99. if self.recordingThread != None:
  100. self.recordingThread.stop()

obama_img = face_recognition.load_image_file("obama.jpg")  ,我这里只加了一张奥巴马的图片作为人脸库,这张图片你可以拍一张自己的照片替换,放在目录下就好了,没有照片会报错的。

结尾

加人脸识别的地方,可以改成调用某些平台的AI图片处理API,自行封装哈。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/709684
推荐阅读
相关标签
  

闽ICP备14008679号