post_image.py

发布时间 2023-10-06 21:57:49作者: 苦逼yw

说明:在特定的目录中通过文件名通配符筛选出图片,向某个接口发送post请求传输图片识别图片,并判断post请求返回的结果

 

#!/usr/bin/python
#-*- coding: utf-8 -*-
import os, sys, glob
#import tqdm
import multiprocessing
#import re
import time
import threading
import requests
import urllib
import logging
import json
import base64
import random
import math
#import queue
import datetime
import cv2
import uuid
import pprint

#fcgi_addr = "http://127.0.0.1:38899/api/image/process"
fcgi_addr = "http://68.109.211.72:38899/api/image/process"
#fcgi_addr = "http://127.0.0.1:2019/api/image/process"
#url = "http://127.0.0.1:2019/api/image/process"
url = "http://68.109.211.53:2019/api/image/process"

base_dir = os.path.dirname(os.path.abspath(__file__))

image_folders = "../data/1"
save_dir = image_folders + "_pred/"
max_pics_num = 0

def mkdir(path):
    if not (os.path.exists(path)):
        os.makedirs(path)
    
def get_path(path):
    
    file_list = glob.glob(os.path.join(path, "5.jpg")) # RJywh2JU7FmIOkb1AAxhkxyeNnUAc1jhgPueQEADGGr995
    return file_list

def process_srv_fusion(image_path, files):
    task = {
        "max_analysis_byc":0,
        "max_analysis_per":0,
        "max_analysis_tri":0,
        "max_analysis_veh":1, 
        "odac_min_area_veh":100, 
        "odac_min_area_byc":81,
        "odac_min_area_tri":100, 
        "od_min_area":64, 
        "enable_bycycle_analysis":1,
        "enable_tricycle_analysis":1,
        "enable_tricycle_lp_analysis":1,
        "enable_vehicle_analysis":1, 
        "enable_person_analysis":0, 
        "enable_vehicle_lp_analysis":1,
        "switch_od":1, 
        "switch_vehprop":1,
        "switch_bycprop":1, 
        "switch_triprop":1,
        "switch_perprop":1,
        "switch_lp":1,
        "switch_vrd":1,
        "switch_vm":1,
        "vda_lpd_threshold":0.05,
        "vda_lpr_threshold":0.1
        # "vda_default_local_abbr_threshold":0.9,
    }
    res = requests.post(url = url, files=files, data = task)
    res.encoding = "utf-8"
    #print(res.text)
    json_r = json.loads(res.text)
    print(json_r)  
    return json_r                

def process(image_path):
    # print(image_path)
    # img = cv2.imread(image_path)
    files = []
    img_data = ""
    with open(image_path, "rb") as file: 
        img_data = file.read()
    #print("img_type: {}".format(type(img_data)))
    files.append(("image_file", ("image", img_data, "image/jpg")))
    json_r = process_srv_fusion(image_path, files)
    if "data" in json_r and "objects" in json_r["data"]:
        objects = json_r["data"]["objects"]
        for object_ in objects:
            if "attrs" in object_ and "detection" in object_:
                payload = {
                        "switch_lpr": 1,
                        "odr_x": int(object_["detection"]["x"]),
                        "odr_y": int(object_["detection"]["y"]),
                        "odr_w": int(object_["detection"]["w"]),
                        "odr_h": int(object_["detection"]["h"]),
                        }
                r = requests.post(fcgi_addr, files=files, data=payload)
                #print("r.text: {},type: {}".format(r.text,type(r.text)))
                try:
                        print('\nlicense_plates: {}'.format(json.loads(r.text)['license_plates']))
                except:
                        print('license_plates is NULL')
                print('------------------------------------------------------------')
if __name__ == "__main__":
    #image_path_list = get_path(image_folders)
    mkdir(save_dir)
    #random.shuffle(image_path_list)
    #if max_pics_num == 0 or max_pics_num > len(image_path_list):
    #    max_pics_num = len(image_path_list)
    #print("max_pics_num: ", max_pics_num)
    #image_path_list = image_path_l:ist[:max_pics_num]


    start_time = time.time()

    #pool = multiprocessing.Pool(32)
    #pool.map(process, image_path_list[0:])
    #pool.close()
    #pool.join()
    image_path = r'../data/1_pred/tmp_imgs/*.jpg'
    images_path_list = glob.glob(image_path)
    for i in images_path_list:
        print("img: {}".format(os.path.basename(i)))
        process(i)

    end_time = time.time()
    total_time = end_time - start_time
    print("total time: %.5fs" % total_time)


    # for image_path in image_path_list:
    #     process(image_path)