def openfile_button_connect(self): fname = QFileDialog.getOpenFileName(self, '打开文件', './') self.picpath = fname[0] MyUtils.getLogger(__name__).info("打开文件" + self.picpath) if len(self.picpath) < 4 or not (self.picpath[-4:] == '.png' or self.picpath[-4:] == '.jpg'): Alter_Dialog("警报", "请选择jpg或者png文件").exec_() return png = QPixmap(self.picpath) if png.isNull(): Alter_Dialog("警报", "图片转换出现错误").exec_() return # 按与控件的比例,对图像进行缩放 if png.width() / png.height() > self.ui.label.width( ) / self.ui.label.height(): png = png.scaled( self.ui.label.width(), png.height() * self.ui.label.width() / png.width()) else: png = png.scaledToHeight( png.width() * self.ui.label.height() / png.height(), self.ui.label.height()) # 把图像放到控件中显示 MyUtils.getLogger(__name__).info("展示图片" + self.picpath) self.ui.label.setPixmap(png)
def showPicAndResult_connect(self, pic, result, picpath): self.ui.label.setPixmap(pic) self.picpath = picpath self.showResult(result) self.picnum_haveshow += 1 MyUtils.getLogger(__name__).info("展示图片及其结果" + picpath) self.ui.label_4.setText( str(self.picnum_haveshow) + "/" + str(self.picnum_toshow)) if (self.picnum_haveshow == self.picnum_toshow): self.ui.pushButton_6.setText("展示识别结果")
def analysisCurPic_button_connect(self): if len(self.picpath) < 4 or not (self.picpath[-4:] == '.png' or self.picpath[-4:] == '.jpg'): Alter_Dialog("警报", "请选择jpg或者png文件").exec_() else: try: self.showResult(MyUtils.getPicAnalysisResult(self.picpath)) MyUtils.getLogger(__name__).info("展示图片分析结果" + self.picpath) except Exception as e: Alter_Dialog(e.__str__()).exec()
def doAllFileinThread_threadfunction(self, dfb, havedonedPath, undoPath): i = (int)(len(os.listdir(havedonedPath)) / 2) j = i num_undo = len(os.listdir(undoPath)) if num_undo == 0: dfb.ui.progressBar.hide() dfb.ui.label.setText("没有文件待处理") dfb.ui.pushButton.show() return for root, dirs, files in os.walk(self.undoPath): for file in files: filepath = os.path.join(root, file) MyUtils.getLogger(__name__).info("读取 " + filepath) # 判断用户选择的文件是否符合要求,这里只支持png和jpg if len(filepath) < 4 or (not (filepath[-4:] == '.png' or filepath[-4:] == '.jpg')): MyUtils.getLogger(__name__).warn(file + "不是jpg或者png文件") else: result = "" try: result = MyUtils.getPicAnalysisResult(filepath) except Exception as e: MyUtils.getLogger(__name__).error(e.__str__()) os.rename( filepath, os.path.join(self.havedonedPath, str(i)) + filepath[-4:]) try: fjson = open( os.path.join(self.havedonedPath, str(i)) + ".json", 'w') fjson.write(result) i += 1 fjson.close() except Exception as e: MyUtils.getLogger(__name__).error(e.__str__() + " open " + self.havedonedPath + " failed") dfb.ui.progressBar.setValue(100 * (i - j) / num_undo) dfb.ui.progressBar.setValue(100) dfb.ui.label.setText("识别已经完成") dfb.ui.pushButton.show()
def run(self): for root, dirs, files in os.walk(self.havedonedPath): for file in files: while self.thread_status == 0: self.sleep(5) if self.thread_status == 1: filepath = os.path.join(root, file) if len(filepath) < 4 or not (filepath[-4:] == '.png' or filepath[-4:] == '.jpg'): pass else: pic = QPixmap(filepath) if pic.isNull(): MyUtils.getLogger(__name__).error("警报", "图片转换出现错误") else: # 按与控件的比例,对图像进行缩放 if pic.width() / pic.height() > self.wid / self.he: pic = pic.scaled( self.wid, pic.height() * self.wid / pic.width()) else: pic = pic.scaledToHeight( pic.width() * self.he / pic.height(), self.he) try: file = open(filepath[:-4] + '.json') result = file.read() file.close() self.shownew_signal.emit(pic, result, filepath) except Exception as e: MyUtils.getLogger( __name__).error(e.__str__() + " open " + self.havedonedPath + " failed") self.sleep(3) elif self.thread_status == -1: return self.thread_status = -1
def showResult(self, result): resultobj = None try: resultobj = json.JSONDecoder().decode(result) except Exception as e: MyUtils.getLogger(__name__).error(e.__str__()) if (resultobj == None or 'error_message' in resultobj): try: result = MyUtils.getPicAnalysisResult(self.picpath) resultobj = json.JSONDecoder().decode(result) except Exception as e: MyUtils.getLogger(__name__).error(e.__class__ + e.__str__()) if (resultobj == None): text = '数据请求失败' self.ui.label_2.setText(text) return elif ('error_message' in resultobj): text = "error_message:" + resultobj['error_message']['value'] self.ui.label_2.setText(text) return text = '' j = 0 try: for i in resultobj['faces']: text += "人物" + str(j) + ":\n" j += 1 att = i['attributes'] text += "age: " + str(att['age']['value']) text += "\n性别: " + ("女" if att['gender']['value'] == "Female" else "男") text += "\n人种: " if att['ethnicity']['value'] == 'None': text += "亚洲人" elif att['ethnicity']['value'] == 'White': text += "白人" else: text += "黑人" text += "\n笑容程度: " + str( att['smile']['value']) + "\t阈值:" + str( att['smile']['threshold']) + "\n" if att['glass']['value'] == 'None': text += "没有佩戴眼镜" elif att['glass']['value'] == 'Dark': text += "佩戴墨镜" else: text += "佩戴普通眼镜" text += '\n' except Exception as e: MyUtils.getLogger(__name__).error(e.__class__ + e.__str__()) self.ui.label_2.setText(text)
def showResult(self, result, picpath): resultobj = None try: resultobj = json.JSONDecoder().decode(result) except Exception as e: MyUtils.getLogger(__name__).error(e.__str__()) if (resultobj == None or 'error_message' in resultobj): try: result = MyUtils.getPicAnalysisResult(self.picpath) resultobj = json.JSONDecoder().decode(result) except Exception as e: MyUtils.getLogger(__name__).error(e.__class__ + e.__str__()) if (resultobj == None): text = '数据请求失败' self.ui.label_2.setText(text) return elif ('error_message' in resultobj): text = "error_message:" + resultobj['error_message']['value'] self.ui.label_2.setText(text) return text = '' j = 1 try: for i in resultobj['faces']: text += "人物" + str(j) + ":\n" j += 1 att = i['attributes'] text += "年龄: " + str(att['age']['value']) text += "\n性别: " + ("女" if att['gender']['value'] == "Female" else "男") text += "\n人种: " if att['ethnicity']['value'] == 'ASIAN' or att['ethnicity'][ 'value'] == 'Asian': text += "亚洲人" elif att['ethnicity']['value'] == 'White': text += "白人" else: text += "黑人" text += "\n笑容程度: " + str(att['smile']['value']) + "\n" if att['glass']['value'] == 'None': text += "没有佩戴眼镜" elif att['glass']['value'] == 'Dark': text += "佩戴墨镜" else: text += "佩戴普通眼镜" text += '\n' point_y = i['face_rectangle']['top'] point_x = i['face_rectangle']['left'] height = i['face_rectangle']['height'] width = i['face_rectangle']['width'] point1 = (point_x, point_y) point2 = (point_x + width, point_y) point3 = (point_x, point_y + height) point4 = (point_x + width, point_y + height) img = Image.open(picpath) imgd = ImageDraw.Draw(img) imgd.line([point1, point2, point4, point3, point1], fill=(0, 255, 0), width=5) img.show() ''' !!! 目前存在的问题: 1.图片中有多个人脸时会显示多张人脸边框图片 2.带人脸边框的图片无法显示到控件中 3.主界面按钮中文字显示不全 ''' except Exception as e: MyUtils.getLogger(__name__).error(e.__class__ + e.__str__()) self.ui.label_2.setText(text)