服务端
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkobjectdet.request.v20191230.RecognizeVehicleDamageRequest import RecognizeVehicleDamageRequest
import json
import oss2
class req(object):
def __init__(self):
self.info = open('myinfo.json','r')
self._json = json.load(self.info)
self.info.close()
def push_img(self):
auth = oss2.Auth(self._json['accesskey'], self._json['secret'])
bucket = oss2.Bucket(auth,'http://oss-cn-shanghai.aliyuncs.com','current-cloud-oss')
bucket.create_bucket(oss2.models.BUCKET_ACL_PRIVATE)
try:
bucket.delete_object('car/car.jpg')
bucket.put_object_from_file('car/car.jpg', 'car.jpg')
except Exception as e:
print(e)
exit(-1)
def ApiFindDamage(self, url):
client = AcsClient(self._json['accesskey'], self._json['secret'], 'cn-shanghai')
request = RecognizeVehicleDamageRequest()
request.set_accept_format('json')
request.set_ImageURL(url)
self.response = client.do_action_with_exception(request)
# python2: print(response)
return str(self.response, encoding='utf-8')
def get_url(self):
from viapi.fileutils import FileUtils
file_utils = FileUtils(self._json['accesskey'], self._json['secret'])
oss_url = file_utils.get_oss_url("http://xxx.oss-shanghai.jpg", "jpg", False)
return oss_url
其中包括了连接oss,上传本地图片,生成oss链接,调用API实现对图片的鉴定。
视图
# from PySide2.QtWidgets import QDialog, QLabel, QGroupBox, QTextEdit, QPushButton, QCheckBox
from PIL import Image
from PySide2.QtWidgets import *
from PySide2.QtGui import *
from qtObj.widgetPickServer import *
from sofulan import req
import requests
class GUI(QWidget):
def __init__(self):
super().__init__()
self.resize(600,400)
self.setWindowTitle('车辆定损')
self.mainlayout = QVBoxLayout()
self.set_widgt()
self.service = req()
self.setLayout(self.mainlayout)
self.show()
self.filter = {1: '轻微刮擦', 2: '重度刮擦', 3: '轻度变形', 4: '中度变形', 5: '重度变形', 6: '未知损伤', 7: 'crack破损孔洞', 8: '翼子板保险杠缝隙', 9: '未知损伤', 10: '位置损伤'}
def set_widgt(self):
self.textarea = QTextEdit(self)
self.textarea.setText('请输入url')
self.textarea.resize(200,100)
self.commitbutton = QPushButton(self)
self.commitbutton.setText("确定")
self.commitbutton.clicked.connect(self.get_req)
self.mainlayout.addWidget(self.textarea)
self.mainlayout.addWidget(self.commitbutton)
@staticmethod
def cropImage(name, boxs):
box = tuple(boxs)
img = Image.open('resouce.jpg')
cropped = img.crop(box)
cropped.save(name+'.jpg')
def get_req(self):
url = self.textarea.toPlainText()
with open('resouce.jpg','wb') as f:
res = requests.get(url)
f.write(res.content)
text = self.service.ApiFindDamage(url)
# 创建水平布局
widget = QWidget()
new_widget_layout = QHBoxLayout()
# widget.setLayout()
# 取值
text = json.loads(text)
data = text['Data']
elements = data['Elements']
for i in elements:
crack_type = i['Type']
score = i['Scores']
box = i['Boxes']
print(self.filter[int(crack_type)],end='\t')
# print(score)
for j in range(len(score)):
if score[j] > 0:
label = QLabel(self)
print('损伤可能性:{}'.format(score[int(j)]))
self.cropImage(self.filter[int(crack_type)]+str(score[int(j)]), box)
# 添加子控件图片
pix = QPixmap(self.filter[int(crack_type)]+str(score[int(j)])+'.jpg')
label.setPixmap(pix)
label.resize(pix.width(),pix.height())
# label.setText(self.filter[int(crack_type)]+" 可能性"+str(score[int(j)]))
# ls.append(self.label)
new_widget_layout.addWidget(label)
widget.setLayout(new_widget_layout)
self.mainlayout.addWidget(widget)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
gui = GUI()
sys.exit(app.exec_())
利用pyside2制作的窗口程序,根据box进行图片裁剪,展现损伤位置。
(脚本截图)