首页 > 其他分享 >post_image.py

post_image.py

时间:2023-10-06 22:00:50浏览次数:32  
标签:import image py list time print path post

说明:在特定的目录中通过文件名通配符筛选出图片,向某个接口发送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)

 

标签:import,image,py,list,time,print,path,post
From: https://www.cnblogs.com/lfxx/p/17745146.html

相关文章

  • 无涯教程-OC - Image View函数
    ImageView用于显示单个图像或动画序列。ImageView-重要属性imageHighlightingImageuserInteractionEnabledanimationImagesanimationRepeatCountImageView-重要方法-(id)initWithImage:(UIImage*)image-(id)initWithImage:(UIImage*)imagehighlightedIm......
  • PostgreSQL 的模式匹配与正则表达式
    一、PostgreSQL实现模式匹配的方法LIKESIMILARTOPOSIX风格的正则表达式模式匹配函数substring二、LIKE操作符只有在匹配整个字符串时返回真符号描述%任意0个或任意个字符_任意一个字符\%%\__postgres=#select*fromtest_zhengze;id|......
  • 笨办法学Python3 习题24 更多的练习
    根据书中的PowerShell运行结果,进行仿写 beans,jars,crates=secret_formula(start_point)#函数运算结果存储方式一print(f"We'dhave{beans}beans,{jars}jars,and{crates}crates.") formula=secret_formula(start_point)        #两种函数运......
  • python单例模式
    Python单例模式的好处主要有以下几点:节省资源:单例模式可以确保一个类只有一个实例,这样可以避免在多个地方创建相同的对象,从而节省内存和计算资源。保证数据一致性:在多线程环境下,单例模式可以确保全局变量只被初始化一次,避免了多线程同时修改数据导致的数据不一致问题。方......
  • python - pdf转成excel文件
    初次尝试用python将pdf转换为excel表格,如有错误欢迎指出,需要用到的库如下:pipinstallpdfminer3kpipinstalltabula-pypipinstallopenpyxl如果是pip3,则:pip3installpdfminer3kpip3installtabula-pypip3installopenpyxl通过终端即可安装新建一个IDLE文件,源码如下:......
  • Python异步编程并发比较之循环、进程、线程、协程
    服务端现在有一个api接口http://127.0.0.1:18081/hello批量请求该接口,该接口中有一个5s的阻塞。使用循环,多进程,多线程,协程等四种方式,一共请求10次,比较总的请求耗时。importtimefromflaskimportFlaskapp=Flask(__name__)@app.route('/hello')defhello_world():......
  • Python分享之路径与文件 (os.path包, glob包)
    os.path包os.path包主要是处理路径字符串,比如说'/home/vamei/doc/file.txt',提取出有用信息。importos.pathpath='/home/vamei/doc/file.txt'print(os.path.basename(path))#查询路径中包含的文件名print(os.path.dirname(path))#查询路径中包含的目录info=......
  • # yyds干货盘点 # 盘点一个Python自动化办公实战实现数据汇总填充(方法四)
    大家好,我是皮皮。一、前言前几天在Python最强王者交流群【哎呦喂 是豆子~】问了一个Python自动化办公的问题,一起来看看吧。下图是他的原始数据和他想得到的目标数据,如下所示:需要在标黄的两行里边进行相关操作。二、实现过程之前的文章中【莫生气】使用了openpyxl进行了实现,的确可......
  • python11
    3.3字符串的公共功能1.相加:字符串+字符串v1="吉林省"+"长春市"print(v1)2.相乘:字符串*整数data="alex"*3print(data)3.长度data="吉林省长春市"value=len(data)print(value)4.获取字符串中的字符,索引message="吉林省长春市"#012......
  • Python使用socket的UDP协议实现FTP文件服务
    简介本示例主要是用Python的socket,使用UDP协议实现一个FTP服务端、FTP客户端,用来实现文件的传输。在公司内网下,可以不适用U盘的情况下,纯粹使用网络,来实现文件服务器的搭建,进而实现文件的网络传输。同时用来理解Python的socket使用。服务端运行起来后,会把服务器上面的指......