Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use python to realize Surveillance Video population Statistics

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use python to achieve surveillance video population statistics". In daily operation, I believe that many people have doubts about how to use python to achieve surveillance video population statistics. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "how to use python to achieve surveillance video population statistics". Next, please follow the editor to study!

1. Illustration

The client requests to input a video or a video stream, output the number of people or other targets, and report it to the upper server, that is, to provide a http API call algorithm to count the number of people. Finally, http reports the total number of people.

2. Preparation

Related technology python pytorch opencv http protocol post request

Flask

Flask is a Python micro-framework for web development, which is relatively easy to use for people like me who are not familiar with the web framework.

Flask installation

Sudo pip install Flask III. A simple server application

To get a glimpse of how flask is used, let's start with a simple server example.

The first file, hello.py.

From flask import Flaskapp = Flask (_ _ name__) @ app.route ("/") def hello (): return 'hello worldview' @ app.route ("/ python") def hello_python (): return 'hello pythonium' If _ _ name__ = ='_ main__': app.run (host='0.0.0.0')

App.run (host='0.0.0.0') indicates that the ip is now set to 0.0.0.0, and it is very convenient to set it to 0.0.0.0. If you are setting up the server on a remote computer and the ip of that remote computer is 172.1.1.1, you can set ip to 172.1.1.1 on the local computer to make a request to the server.

@ app.route ('/') indicates that the address to send request is http://0.0.0.0:5000/ python app.route ("/ python") indicates that the address to send requests is http://0.0.0.0:5000/python.

The second file is 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) 4. Send pictures to the server

Server code

# coding:utf-8from flask import request, Flaskimport osapp = 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)

Client code

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.textprint result

In this case, long-distance pictures are the fastest, much faster than using opencv to open first and then transfer pixel-level numbers.

5. The final key yolov5 call code: #! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 2021-2-20 18 Time @ Author: xiaorun# @ Site: # @ File: yoloDetect.py# @ Software: PyCharmimport sysimport threadingfrom threading import Threadimport timeimport osimport cv2from yolo import YOLO5import json Jsonifyimport requestsimport flaskfrom flask import requestheaders = {'Content-Type':' application/json'} url_addr= "http://123.206.106.55:8065/api/video/getPersonNum/"# creates a service Think of the current python file as a service server = flask.Flask (_ _ name__) server.debug = Truedef 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 to read video Size 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) # read video duration (total frames) 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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report