前言
本來想做内容安全方面的識别,但是由于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_())