Python

159 阅读2分钟

开启多线程

from threading import Thread, enumerate,current_thread
import time


def sing():
    for i in range(3):
        print("singing %d" % i)
        time.sleep(1)
def dance():
    for i in range(3):
        print("dancing %d" % i)
        time.sleep(1)
def main():
    thread_sing = Thread(target=sing)  # 创建线程
    thread_dance = Thread(target=dance)

    thread_sing.start()  # 启动线程
    thread_dance.start()

    for thread in enumerate():  # 枚举所有线程(包括主线程)
        print(thread)

    thread = current_thread()  # 获取当前线程
    print(thread.ident)  # 获取线程id

if __name__ == '__main__':
    main()

开启多个线程进行网络请求

from threading import Thread, enumerate,current_thread
import time
from requests import request

def sing():
    url = "http://127.0.0.1:10050/measure"
    with open("/home/tang/Videos/30s.mp4", "rb") as file:
        files = [('video', file)]
        request("POST", url, files=files)
        # print(response.text.encode('utf8'))
        file.seek(0)

def main():
    for i in range(15):
        print(i)
        thread_sing = Thread(target=sing)
        thread_sing.start()  # 启动线程
        time.sleep(12)
        thread = current_thread()  # 获取当前线程
        print(thread.ident)  # 获取线程id

if __name__ == '__main__':
    main()

通过post多次发送同一文件

from requests import request

def main():
    url = "http://127.0.0.1:10050/measure"
    with open("/home/tang/Videos/30s.mp4", "rb") as file:
        files = [('video', file)]
        i = 0
        while i < 1000:
            request("POST", url, files=files)
            file.seek(0)
            i += 1


if __name__ == '__main__':
    main()

file.seek(0) 移动到文件头部

停止Thread中的死循环

import threading
import time
def aa(stop):
	while True:
		print('thread running')
		if stop():
				break
def main():
	stop_threads = False
	t1 = threading.Thread(target = aa, args =(lambda : stop_threads, ))
	t1.start()
	time.sleep(1)
	stop_threads = True
	t1.join()
	print('thread killed')
main()

安装redis

pip install redis

ffmpeg rtsp转rtmp推流

if __name__ == "__main__":
    rtsp_server = 'rtmp://192.168.0.202:1935/stream/tang'  # push server (output server)

    # pull rtsp data, or your cv cap.  (input server)
    cap = cv2.VideoCapture(
        'rtsp://admin:12345qwert@192.168.0.5:554/h264/ch1/main/av_stream')

    sizeStr = str(int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))) + \
              'x' + str(int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
    fps = int(cap.get(cv2.CAP_PROP_FPS))

    command = ['ffmpeg',
               '-y',
               '-f', 'rawvideo',
               '-vcodec', 'rawvideo',
               '-pix_fmt', 'bgr24',
               '-s', sizeStr,
               '-r', str(fps),
               '-i', '-',
               '-pix_fmt', 'yuv420p',
               '-f', 'flv',
               rtsp_server]

    p = sp.Popen(command, stdin=sp.PIPE)

    while True:
        ret, frame = cap.read()
        if not ret:
            print("Opening camera is failed")
            break
        p.stdin.write(frame.tostring())

ffmpeg rtsp保存到本地视频(python)

import ffmpeg
host = '172.28.51.122'
(
    ffmpeg
    # .input('rtsp://' + 'user:password@' + host)
    .input('rtsp://admin:12345qwert@192.168.0.5:554/h264/ch1/main/av_stream')
    .output('saved_rtsp.mp4')# 保存的文件名
    .overwrite_output() # 覆盖同名文件
    .run(capture_stdout=True)# 运行保存
)

关于python安装第三方库速度慢解决方案(opencv为例)

安装时使用国内镜像链接

阿里云 mirrors.aliyun.com/pypi/simple…

中国科技大学 pypi.mirrors.ustc.edu.cn/simple/

豆瓣(douban) pypi.douban.com/simple/

清华大学 pypi.tuna.tsinghua.edu.cn/simple/

中国科学技术大学 pypi.mirrors.ustc.edu.cn/simple/

使用指令指定源:

pip install -i pypi.tuna.tsinghua.edu.cn/simple opencv-python

秒秒种成功安装

还有scarpy安装很烦 所以使用conda install scrapy也很快(必须安装anaconda)

转载:blog.csdn.net/angchi9848/…

Python MongoDB 教程

www.qikegu.com/docs/3312

mongodb 储存numpy数组

blog.csdn.net/qq_34877350…

python中grpc的使用示例

www.cnblogs.com/zongfa/p/12…

批量读取电脑文件夹内视频,获取视频分辨率,过滤删除分辨率较小的文件

blog.csdn.net/li_ji_an/ar…

python 获取多线程的返回值

www.cnblogs.com/tianleblog/…

[os]分离文件目录,文件名以及文件后缀

blog.csdn.net/yinxingtian…

人脸识别

Face Recognition

choerodon.io/zh/blog/fac…

人脸相似度对比(dlib)

blog.csdn.net/m0_38106923…

将facenet的128维encoding存入/度出数据库

blog.csdn.net/weixin_4197…

利用MTCNN和facenet实现人脸检测和人脸识别

blog.csdn.net/guyuealian/…

opencv之dlib库人脸识别

www.cnblogs.com/ywjfx/p/114…

opencv之dlib库人脸识别 www.cnblogs.com/ywjfx/p/114…

gitee.com/steinven/Fa…