python - 来自响应请求python的图像中的标记框

标签 python opencv python-requests response

import requests
import cv2
frame=cv2.imread('C:\\Users\\aaa\\Downloads\\abc.jpg')
url = 'https://app.nanonets.com/api/v2/ObjectDetection/Model/4729a79f-ab19-4c1b-8fe9'

data = {'file': open('C:\\Users\\aaa\\Downloads\\abc.jpg', 'rb')}

response = requests.post(url, auth=requests.auth.HTTPBasicAuth('S5zsN-yFZJxRH9tMwsaHUCxJg3dZaDWj', ''), files=data)
print (type(response))
print(response)

我上传了一张用于物体检测的图像。我得到了这样的回应。
{"message":"Success","result":[{"message":"Success","input":"abc.jpg","prediction":[{"label":"car","xmin":411,"ymin":332,"xmax":585,"ymax":462,"score":0.99097943},{"label":"car","xmin":496,"ymin":170,"xmax":592,"ymax":248,"score":0.96399206},{"label":"car","xmin":223,"ymin":147,"xmax":294,"ymax":202,"score":0.9383388},{"label":"car","xmin":164,"ymin":130,"xmax":230,"ymax":175,"score":0.8968652},{"label":"car","xmin":448,"ymin":489,"xmax":623,"ymax":540,"score":0.8311123}],"page":0,"request_file_id":"5a8549f1-fb2c-487a-83b5-234608b3168b","filepath":"uploadedfiles/4729a79f-ab19-fac0fb807e6d/PredictionImages/53207.jpeg"}]}

我想在图像中使用给定的坐标框。

最佳答案

import requests
import cv2
import math 
from PIL import Image, ImageDraw 
import json

frame=cv2.imread('C:\Users\aaa\Downloads\abc.jpg')
url = 'https://app.nanonets.com/api/v2/ObjectDetection/Model/4729a79f-ab19-4c1b-8fe9'
data = {'file': open('C:\Users\aaa\Downloads\abc.jpg', 'rb')}
response = requests.post(url, auth=requests.auth.HTTPBasicAuth('S5zsN-yFZJxRH9tMwsaHUCxJg3dZaDWj', ''), files=data)

predictions = json.loads(response.text)['result'][0]['prediction']
img = Image.open('C:\Users\aaa\Downloads\abc.jpg')
img1 = ImageDraw.Draw(img)   

for prediction in predictions:  
    shape = [(prediction['xmin'], prediction['ymin']), (prediction['xmax'], prediction['ymax'])]     
    img1.rectangle(shape, fill ="# ffff33", outline ="red") 
img.show() 

关于python - 来自响应请求python的图像中的标记框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61607454/

相关文章:

python - 尝试从 Pycharm 安装 sklearn 时出错 | arrayobject.h 不能是绝对的

c++ - OpenCV accumulatedWeight 错误( channel 和大小比较断言失败)

c# - 将 Mat 转换为 Android 的 ImageSource OpenCV

python - 当从 FastAPI 发送时流主体为满,当由请求接收时流主体为空

python - 为什么 np.nan_to_num() 不转换这个 (n x m) 数组?

python - Tensorflow:将张量作为一个整体进行字符串化(不创建字符串张量)

python - Twisted 和 ICMP (txNetTools)

python - OpenCV KMeans(K-Means)python输出簇数问题

python - 检查 session 是否仍然有效/处理 connectionError 的最有效方法?

Python 请求 POST 命令不遵循 302 重定向