您好,登錄后才能下訂單哦!
這篇文章主要介紹“如何用python實現監控視頻人數統計”,在日常操作中,相信很多人在如何用python實現監控視頻人數統計問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何用python實現監控視頻人數統計”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
客戶端請求輸入一段視頻或者一個視頻流,輸出人數或其他目標數量,上報給上層服務器端,即提供一個http API調用算法統計出人數,最終http上報總人數
相關技術 python pytorch opencv http協議 post請求
Flask
Flask是一個Python實現web開發的微框架,對于像我對web框架不熟悉的人來說還是比較容易上手的。
Flask安裝
sudo pip install Flask
為了稍微了解一下flask是如何使用的,先做一個簡單的服務器例子。
第一個文件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')表示現在設定的ip為0.0.0.0,并且設定為0.0.0.0是非常方便的,如果你是在一臺遠程電腦上設置服務器,并且那臺遠程電腦的ip是172.1.1.1,那么在本地的電腦上可以設定ip為172.1.1.1來向服務器發起請求。
@app.route('/')表示發送request的地址是http://0.0.0.0:5000/,@app.route("/python")表示發送requests的地址為http://0.0.0.0:5000/python。
第二個文件是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)
服務器代碼
#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先打開后傳遞象素級的數字要快很多.
#!/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/"
# 創建一個服務,把當前這個python文件當做一個服務
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: {}
size: {}".format(fps, size))
# 讀取視頻時長(幀總數)
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: # 檢測是否有數據
return ('fail..')
video_name= request.data.decode('utf-8')
# 獲取到POST過來的數據,因為我這里傳過來的數據需要轉換一下編碼。根據晶具體情況而定
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)
# 把區獲取到的數據轉為JSON格式。
data_return={"code":200,"data":url_video,"message":"請求成功","sucsess":"true"}
return json.dumps(data_return)
else:
pass
# 返回JSON數據。
if __name__ == '__main__':
server.run(host='192.168.1.250', port=8888)
客戶端請求測試:
#!/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)
到此,關于“如何用python實現監控視頻人數統計”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。