从rtmp拉流后进行python鼻子定位demo
本文讲述从在线直播拉流中通过dlib定位鼻子的demo
拉流地址:rtmp://58.200.131.2:1935/livetv/hunantv 湖南卫视
效果:
代码较容易,主逻辑都是和普通本地摄像头一样的,dlib部分要重点看看:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | import cv2 import threading import dlib import imutils from imutils import face_utils predictor_path = 'models\\shape_predictor_68_face_landmarks.dat' face_rec_model_path = 'models\\dlib_face_recognition_resnet_model_v1.dat' predictor = dlib.shape_predictor(predictor_path) detector = dlib.get_frontal_face_detector() (noseStart, noseEnd) = face_utils.FACIAL_LANDMARKS_IDXS[ "nose" ] class Producer(threading.Thread): """docstring for Producer""" def __init__( self , rtmp_str): super (Producer, self ).__init__() self .rtmp_str = rtmp_str # 通过cv2中的类获取视频流操作对象cap self .cap = cv2.VideoCapture( self .rtmp_str) # 调用cv2方法获取cap的视频帧(帧:每秒多少张图片) # fps = self.cap.get(cv2.CAP_PROP_FPS) self .fps = self .cap.get(cv2.CAP_PROP_FPS) print ( self .fps) # 获取cap视频流的每帧大小 self .width = int ( self .cap.get(cv2.CAP_PROP_FRAME_WIDTH)) self .height = int ( self .cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) self .size = ( self .width, self .height) print ( self .size) def run( self ): print ( 'in producer' ) ret, image = self .cap.read() while ret: frame = imutils.resize(image, width = 600 ) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) rects = detector(gray, 0 ) for rect in rects: shape = predictor(gray, rect) shape = face_utils.shape_to_np(shape) nose = shape[noseStart:noseEnd] noseHull = cv2.convexHull(nose) cv2.drawContours(frame, [noseHull], - 1 , ( 0 , 255 , 0 ), 1 ) cv2.putText(frame, "nose" , (nose[ 0 ][ 0 ], nose[ 0 ][ 1 ]), cv2.FONT_HERSHEY_SIMPLEX, 0.7 , ( 0 , 0 , 255 ), 2 ) cv2.imshow( "Frame" , frame) cv2.waitKey( int ( 1000 / int ( self .fps))) # 延迟 if cv2.waitKey( 1 ) & 0xFF = = ord ( 'q' ): self .cap.release() cv2.destroyAllWindows() break ret, image = self .cap.read() if __name__ = = '__main__' : print ( 'run program' ) rtmp_str = 'rtmp://58.200.131.2:1935/livetv/hunantv' # 湖南卫视 producer = Producer(rtmp_str) producer.start() |
需要安装的python库
1 2 3 | pip install imutils pip install opencv-python pip install dlib |
依赖的dlib模型文件:
链接:https://pan.baidu.com/s/1hw0bznAO7-7f1AIvYxzVBA
提取码:8691
自省推动进步,视野决定未来。
心怀远大理想。
为了家庭幸福而努力。
商业合作请看此处:https://www.magicube.ai
心怀远大理想。
为了家庭幸福而努力。
商业合作请看此处:https://www.magicube.ai
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】