Build an online image processing server using flash

Task description

The robot needs to take pictures and obtain detection results during operation. The computer processing on the robot is too slow, so it wants to put the image processing program on the server. When the robot needs detection, it sends the captured images and processing requirements to the server. After processing, the server sends the detection results back to the robot.

system architecture

One server, one independent computer, two computers need to be under the same LAN

code

1. Server master file
# -*- coding: UTF-8 -*-

from flask import request, Flask
import os
import cv2
from traffic.TrafficNet import TrafficNet
app = Flask(__name__)	# Must write

def trafficPredict(img_path):
    pred_ret = trafficNet.predict(img_path)	# Call image processing function
    return pred_ret

@app.route("/", methods=['POST'])	
def get_frame():	# When the client accesses through the port, it will call this function directly
    upload_file = request.files['file']		# Get data according to key
    file_name = upload_file.filename		# Get file name
    file_path = os.path.join('/home/wangdx/research/mir_robot/server/getImgs', file_name)

    if upload_file:	# If you receive a file, save the download first
        upload_file.save(file_path)
        result = trafficPredict(file_path)	# Image processing entrance

        toClient = str({'cls': int(result)})
        print("success")
        return toClient
    else:
        return 'failed'


if __name__ == "__main__":
    trafficNet = TrafficNet()	# Neural network initialization
    app.run("0.0.0.0", port=1212)	# Set IP and port

Attention
The IP of the server can be set to "0.0.0.0", so that the client can access it through the real IP of the server; The port is set arbitrarily and is not occupied by other programs.

2. Client master file
import requests
import os
import time


url = "http://180.201.5.159:1212" 	#  IP address of the server
files = os.listdir('./demo')	# Folder where I store test images
file_dirs = [os.path.join('./demo', f) for f in files]

start_time = time.time()	# Start timing

for img in file_dirs:
    file = open(img, 'rb')	# Using open to read files is faster than sending image data directly
    files = {'file': (os.path.basename(img), file, 'image/ppm')}	# Build send format

    r = requests.post(url, files=files)	# Send and get the returned results
    result = r.text		# Get the sent string, and then convert the string to other formats that can be processed

    print(result)

sum_t = time.time() - start_time
print('sum time: ', sum_t)

be careful
When accessing the server, do not write the IP"0.0.0.0" set by the server. Write the real IP of the server. The IP of my server is 180.201.5.159

For the complete program, please refer to my github:

https://github.com/dexin-wang/flask_communication_py

The client sends the image in ppm format to the server, the server runs traffic sign recognition, and then sends the recognition result back to the client. The images in the demo are of category 0. The dataset used is BelgiumTSC at:

https://btsd.ethz.ch/shareddata/

For other basic uses of Flask, please refer to:

https://blog.csdn.net/stesha_chen/article/details/82768510

Tags: Python image processing Flask

Posted by ChrisF79 on Tue, 24 May 2022 09:32:26 +0300