最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

用python實(shí)現(xiàn)監(jiān)控視頻人數(shù)統(tǒng)計(jì)

 更新時(shí)間:2021年05月21日 17:19:59   作者:xiao__run  
今天教各位小伙伴學(xué)習(xí)怎么用python實(shí)現(xiàn)監(jiān)控視頻人數(shù)統(tǒng)計(jì),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴有很大的幫助,需要的朋友可以參考下

一、圖示

在這里插入圖片描述
在這里插入圖片描述

客戶端請(qǐng)求輸入一段視頻或者一個(gè)視頻流,輸出人數(shù)或其他目標(biāo)數(shù)量,上報(bào)給上層服務(wù)器端,即提供一個(gè)http API調(diào)用算法統(tǒng)計(jì)出人數(shù),最終http上報(bào)總?cè)藬?shù)

二、準(zhǔn)備

相關(guān)技術(shù) python pytorch opencv http協(xié)議 post請(qǐng)求

Flask

Flask是一個(gè)Python實(shí)現(xiàn)web開發(fā)的微框架,對(duì)于像我對(duì)web框架不熟悉的人來說還是比較容易上手的。

Flask安裝

sudo pip install Flask

三、一個(gè)簡單服務(wù)器應(yīng)用

為了稍微了解一下flask是如何使用的,先做一個(gè)簡單的服務(wù)器例子。

第一個(gè)文件hello.py。

from flask import Flask
app = Flask(__name__)
 
@app.route("/")
def hello():
  return 'hello world!'
 
@app.route("/python")
def hello_python():
  return 'hello python!'
 
if __name__ == '__main__':
  app.run(host='0.0.0.0')

app.run(host=‘0.0.0.0')表示現(xiàn)在設(shè)定的ip為0.0.0.0,并且設(shè)定為0.0.0.0是非常方便的,如果你是在一臺(tái)遠(yuǎn)程電腦上設(shè)置服務(wù)器,并且那臺(tái)遠(yuǎn)程電腦的ip是172.1.1.1,那么在本地的電腦上可以設(shè)定ip為172.1.1.1來向服務(wù)器發(fā)起請(qǐng)求。

@app.route('/')表示發(fā)送request的地址是http://0.0.0.0:5000/,@app.route("/python")表示發(fā)送requests的地址為http://0.0.0.0:5000/python。

第二個(gè)文件是request.py

import requests
 
url = 'http://0.0.0.0:5000/'
r = requests.get(url)
print(r.status_code)
print(r.text)
 
url = 'http://0.0.0.0:5000/python'
r = requests.get(url)
print(r.status_code)
print(r.text)

四、向服務(wù)器發(fā)送圖片

服務(wù)器代碼

#coding:utf-8
from flask import request, Flask
import os
app = Flask(__name__)
 
@app.route("/", methods=['POST'])
def get_frame():
  upload_file = request.files['file']
  old_file_name = upload_file.filename
  file_path = os.path.join('/local/share/DeepLearning', 'new' + old_file_name)
 
  if upload_file:
      upload_file.save(file_path)
      print "success"
      return 'success'
  else:
      return 'failed'
 
 
if __name__ == "__main__":
    app.run("0.0.0.0", port=5000)

客戶端代碼

import requests
 
url = "http://0.0.0.0:5000"
 
filepath='./t2.jpg'
split_path = filepath.split('/')
filename = split_path[-1]
print(filename)
 
file = open(filepath, 'rb')
files = {'file':(filename, file, 'image/jpg')}
 
r = requests.post(url,files = files)
result = r.text
print result

這種情況長傳圖片是最快的,比用opencv先打開后傳遞象素級(jí)的數(shù)字要快很多.

五、最終關(guān)鍵yolov5調(diào)用代碼:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/2/20 18:19
# @Author  : xiaorun
# @Site    : 
# @File    : yoloDetect.py
# @Software: PyCharm
import sys
import threading
from threading import Thread
import time
import os
import cv2
from yolo import YOLO5
import json,jsonify
import requests
import flask
from flask import request
headers = {'Content-Type': 'application/json'}
url_addr="http://123.206.106.55:8065/api/video/getPersonNum/"

# 創(chuàng)建一個(gè)服務(wù),把當(dāng)前這個(gè)python文件當(dāng)做一個(gè)服務(wù)
server = flask.Flask(__name__)

server.debug = True

def gen_detector(url_video):
    yolo = YOLO5()
    opt = parseData()
    yolo.set_config(opt.weights, opt.device, opt.img_size, opt.conf_thres, opt.iou_thres, True)
    yolo.load_model()
    camera = cv2.VideoCapture(url_video)
    # 讀取視頻的fps,  大小
    fps = camera.get(cv2.CAP_PROP_FPS)
    size = (camera.get(cv2.CAP_PROP_FRAME_WIDTH), camera.get(cv2.CAP_PROP_FRAME_HEIGHT))
    print("fps: {}\nsize: {}".format(fps, size))

    # 讀取視頻時(shí)長(幀總數(shù))
    total = int(camera.get(cv2.CAP_PROP_FRAME_COUNT))
    print("[INFO] {} total frames in video".format(total))
    ret, frame = camera.read()
    if ret==False:
        video_parameter = {"accessKey": "1C7C48F44A3940EBBAQXTC736BF6530342",
                           "code": "0000",
                        "personNum": "video problem.."}
        response = requests.post(url=url_addr, headers=headers, data=json.dumps(video_parameter))
        print(response.json())

    max_person=0
    while total>0:
        total=total-1
        ret,frame=camera.read()
        if ret == True:
            objs = yolo.obj_detect(frame)
            if max_person<=len(objs):
                max_person=len(objs)
            for obj in objs:
                cls = obj["class"]
                cor = obj["color"]
                conf = '%.2f' % obj["confidence"]
                label = cls + " "
                x, y, w, h = obj["x"], obj["y"], obj["w"], obj["h"]
                cv2.rectangle(frame, (int(x), int(y)), (int(x + w), int(y + h)), tuple(cor))
                cv2.putText(frame, label, (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX, 1, cor, thickness=2)
            person = "there are {} person ".format(len(objs))
            cv2.putText(frame, person, (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), thickness=3)
            video_parameter = {"accessKey": "1C7C48F44A3940EBBAQXTC736BF6530342",
                               "code": "0000",
                               "personNum": str(max_person)}
            if total==0:
                response = requests.post(url=url_addr, headers=headers, data=json.dumps(video_parameter))
                print(response.json())
            cv2.imshow("test",frame)
            if cv2.waitKey(1)==ord("q"):
                break

@server.route('/video', methods=['post'])
def get_video():
    if not request.data:  # 檢測是否有數(shù)據(jù)
        return ('fail..')
    video_name= request.data.decode('utf-8')
    # 獲取到POST過來的數(shù)據(jù),因?yàn)槲疫@里傳過來的數(shù)據(jù)需要轉(zhuǎn)換一下編碼。根據(jù)晶具體情況而定
    video_json = json.loads(video_name)
    print(video_json)
    accessKey=video_json["accessKey"]

    if accessKey=="1C7C48F44A3940EBBAQXTC736BF6530342":

        code=video_json["code"]
        url_video=video_json["url"]
        print(url_video)
        gen_detector(url_video)
        # 把區(qū)獲取到的數(shù)據(jù)轉(zhuǎn)為JSON格式。
        data_return={"code":200,"data":url_video,"message":"請(qǐng)求成功","sucsess":"true"}
        return json.dumps(data_return)
    else:
        pass
    # 返回JSON數(shù)據(jù)。

if __name__ == '__main__':
    server.run(host='192.168.1.250', port=8888)

客戶端請(qǐng)求測試:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/5/12 15:12
# @Author  : xiaorun
# @Site    : 
# @File    : test_post.py
# @Software: PyCharm
import requests,json
headers = {'Content-Type': 'application/json'}
user_info = {"accessKey":"1C7C48F44A3940EBBAQXTC736BF6530342",
            "code":"N000001",
            "url":"http:xxxx/video/xxxx.mp4"
            }
r = requests.post("http://8.8.9.76:8888/video",headers=headers, data=json.dumps(user_info))

print (r.text)

到此這篇關(guān)于用python實(shí)現(xiàn)監(jiān)控視頻人數(shù)統(tǒng)計(jì)的文章就介紹到這了,更多相關(guān)python視頻人數(shù)統(tǒng)計(jì)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解python中文編碼問題

    詳解python中文編碼問題

    一直以來python中文編碼是個(gè)及其頭大的問題,需要好好學(xué)習(xí)下,我用python為例,簡單介紹下python編程時(shí)如何處理好中文編碼的問題,感興趣的朋友們可以參考下
    2021-06-06
  • 如何在Python?中使用?Luhn?算法驗(yàn)證數(shù)字

    如何在Python?中使用?Luhn?算法驗(yàn)證數(shù)字

    Luhn 算法驗(yàn)證器有助于檢查合法數(shù)字并將其與不正確或拼寫錯(cuò)誤的輸入分開,這篇文章主要介紹了在Python中使用Luhn算法驗(yàn)證數(shù)字,需要的朋友可以參考下
    2023-06-06
  • Python編程深度學(xué)習(xí)繪圖庫之matplotlib

    Python編程深度學(xué)習(xí)繪圖庫之matplotlib

    今天小編就為大家分享一篇關(guān)于Python編程深度學(xué)習(xí)繪圖庫之matplotlib,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • PyTorch: 梯度下降及反向傳播的實(shí)例詳解

    PyTorch: 梯度下降及反向傳播的實(shí)例詳解

    今天小編就為大家分享一篇PyTorch: 梯度下降及反向傳播的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • Pycharm 設(shè)置自定義背景顏色的圖文教程

    Pycharm 設(shè)置自定義背景顏色的圖文教程

    今天小編就為大家分享一篇Pycharm 設(shè)置自定義背景顏色的圖文教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • 詳解Python3中的迭代器和生成器及其區(qū)別

    詳解Python3中的迭代器和生成器及其區(qū)別

    本篇將介紹Python3中的迭代器與生成器,描述可迭代與迭代器關(guān)系,并實(shí)現(xiàn)自定義類的迭代器模式。非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2018-10-10
  • Python和OpenCV進(jìn)行多尺度模板匹配實(shí)現(xiàn)

    Python和OpenCV進(jìn)行多尺度模板匹配實(shí)現(xiàn)

    本文將實(shí)現(xiàn)如何將標(biāo)準(zhǔn)模板匹配擴(kuò)展到多尺度,使其可以處理模板和輸入圖像大小不同的匹配。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 解決Pycharm后臺(tái)indexing導(dǎo)致不能run的問題

    解決Pycharm后臺(tái)indexing導(dǎo)致不能run的問題

    今天小編就為大家分享一篇解決Pycharm后臺(tái)indexing導(dǎo)致不能run的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • Django 使用logging打印日志的實(shí)例

    Django 使用logging打印日志的實(shí)例

    下面小編就為大家分享一篇Django 使用logging打印日志的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • 關(guān)于Matplotlib繪制動(dòng)態(tài)實(shí)時(shí)曲線的方法改進(jìn)指南

    關(guān)于Matplotlib繪制動(dòng)態(tài)實(shí)時(shí)曲線的方法改進(jìn)指南

    這篇文章主要給大家介紹了關(guān)于Matplotlib繪制動(dòng)態(tài)實(shí)時(shí)曲線的相關(guān)資料,matplotlib是python里最popular的畫圖工具,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-06-06

最新評(píng)論

岱山县| 伊宁县| 积石山| 长沙市| 望谟县| 贵溪市| 泽库县| 西畴县| 江城| 五峰| 临夏市| 武胜县| 包头市| 来凤县| 绥滨县| 广州市| 天台县| 那曲县| 高平市| 鹰潭市| 上思县| 康平县| 吉安县| 临高县| 凌源市| 南丰县| 大新县| 乌拉特前旗| 灵石县| 洪泽县| 杨浦区| 铅山县| 郑州市| 盐池县| 饶河县| 黄浦区| 新沂市| 富裕县| 灵丘县| 松桃| 新野县|