POST一张图片到服务端,服务端调用SSD_Detection.py对图片进行目标检测,返回检测结果(检测框数据)
sever端
#! -*- coding:utf-8 -*-
#!/usr/bin/python
from import BaseHTTPRequestHandler,HTTPServer
import cgi
import cv2
import numpy as np
from SSD_Detection import SSD_Detection #有一个SSD_Detection.py的文件里面放了关于图片目标检测的相关程序,这里不给出这个程序
import socket
detector = SSD_Detection(0.10)#构建检测模型对象
class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
("这是一个http后台服务。".encode())
def do_POST(self):
'''
获取POST的单张图片数据,利用目标检测程序,返回检测结果(json数据)
'''
form = (
fp=,
headers=,
environ={'REQUEST_METHOD':'POST',
'CONTENT_TYPE':['Content-Type'],
}
)
#print("键值:",())
self.send_response(200)
self.end_headers()
imageData = form['file'].value#获取图片数据
#imageData = ('search')
imageNp = (bytearray(imageData), dtype="uint8")
image = (imageNp, cv2.IMREAD_COLOR)
image[:, :, [0, 1, 2]] = image[:, :, [2, 1, 0]]#BGR转RGB
jsonData = (detector.detect_single_image(image))#获得检测结果框
#csvData = (detector.detect_single_image(image))
(())
def main():
try:
server=HTTPServer(('127.0.0.1',8080),MyHandler) #启动服务
print ('welcome to the server.......')
server.serve_forever()# 一直运行
except KeyboardInterrupt:
print ('shuting done server')
()
if __name__=='__main__':
main()
客户端:
#coding=utf-8
import requests
url = 'http://localhost:8080'
path = u'/media/a/DATA/'
#path='./'
print (path)
#files = {'file': ('',open(path, 'rb'))}
files = {'file': open(path, 'rb')}
r = (url, files=files)
print ("检测框:",)