前言
本来想做内容安全方面的识别,但是由于python sdk的不完善加上本人技术有限,便在汽车定损的工作上加以完善,尽可能开发出可以应用的程序。
汽车定损
昨天做了一个简单的ui,本次任务需要在交互方面进行完善,包括选择文件,根据图片透明度显示损伤位置等。
思路
增加可视化,背景显示透明化的车,用返回的box值确定损伤位置,明确损伤程度
由于时间问题,无法将这个项目做到最佳优化,但是对我来说也做到了一定的进步。
全部代码
# 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
import os
class GUI(QWidget):
def __init__(self):
super().__init__()
self.resize(600,400)
self.setWindowTitle('车辆定损')
self.mainlayout = QHBoxLayout()
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('请选择文件')
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)
# x,y = cropped.size
# crop = cropped.resize((int((x*100/y)), 100), Image.ANTIALIAS)
cropped.save('res/'+name+'.jpg')
def get_req(self):
url = 'https://current-cloud-oss.oss-cn-shanghai.aliyuncs.com/car/car.jpg'
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 = QVBoxLayout()
# widget.setLayout()
# 取值
text = json.loads(text)
data = text['Data']
elements = data['Elements']
self.boxes = []
self.fl = []
for i in elements:
crack_type = i['Type']
score = i['Scores']
box = i['Boxes']
self.boxes.append(box)
print(box)
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('res/'+self.filter[int(crack_type)]+str(score[int(j)])+'.jpg')
self.fl.append('res/'+self.filter[int(crack_type)]+str(score[int(j)])+'.jpg')
label.setPixmap(pix)
# 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)
showp = showpic(self.boxes, self.fl, self)
self.mainlayout.addWidget(showp)
class showpic(QDialog):
def __init__(self, box, fl, parent):
super().__init__(parent=parent)
self.box = box
self.fl = fl
self.setimg()
# self.show()
def setimg(self):
self.proccess()
pix = QPixmap('car_after.png')
label0 = QLabel(self)
label0.setPixmap(pix)
self.setGeometry(0, 0, pix.width(), pix.height())
label0.show()
for i in range(len(self.box)):
label = QLabel(self)
# label1 = QLabel(self)
# label1.setText(self.fl[i].replace(".jpg",''))
# label.setGeometry(int((self.box[i][0]+self.box[i][2])/2), int((self.box[i][1])+self.box[i][3]/2),50,50)
ps = QPixmap(self.fl[i])
label.setPixmap(ps)
label.setGeometry(self.box[i][0], self.box[i][1], self.box[i][2], self.box[i][3],)
# left upper width height
def proccess(self):
# if not os.path.isfile('car_after.png'):
img = Image.open("car.jpg")
img = img.convert('RGBA') # 修改颜色通道为RGBA
x, y = img.size # 获得长和宽
# 设置每个像素点颜色的透明度
for i in range(x):
for k in range(y):
color = img.getpixel((i, k))
color = color[:-1] + (100,)
img.putpixel((i, k), color)
img.save("car_after.png") # 要保存为.PNG格式的图片才可以
if __name__ == '__main__':
app = QtWidgets.QApplication([])
gui = GUI()
sys.exit(app.exec_())