def handle(self, message): logging.debug("'LabelFilterConditionHandler' receive message %s", message.to_json()) if message.data is None: return self.replyMessage(message, state=False, msg='请指定要设置的标签筛选条件') data = message.data if 'projectId' not in data or 'action' not in data: return self.replyMessage(message, state=False, msg="请指定要设置的标签筛选条件") if data['projectId'] != self.user.projectId: return self.replyMessage(message, state=False, msg="您不属于当前项目的用户,无法进行操作") context = TLPContext() project = context.get_project(self.user.projectId) # mysql = MysqlManager() try: action = data['action'] connect_id = id(self.websocket) if action == 'set-condition' and "condition" in data and data[ "condition"]: context.set_label_filter_condition(connect_id=connect_id, condition=data["condition"]) return self.replyMessage(message, state=True, msg="设置成功") elif action == 'clean-condition': context.set_label_filter_condition(connect_id=connect_id, condition=None) return self.replyMessage(message, state=True, msg="清除成功") finally: pass
def handle(self, message): ''' @description: @param {type} @return: ''' logging.debug("'AutoAnnotationLabelHandler' receive message %s", message.to_json()) if message.data is None: return self.replyMessage(message, state=False, msg='请指定消息的内容') data = message.data if 'projectId' not in data or 'imageId' not in data or 'inferencerId' not in data: return self.replyMessage(message, state=False, msg="请指定要处理的数据", data=None) if data['projectId'] != self.user.projectId: return self.replyMessage(message, state=False, msg="您没有权限操作此项目") context = TLPContext() project = context.get_project(self.user.projectId) mysql = MysqlManager() try: image_table_name = '`AnnotationProjectImage' + str( project.index) + '`' target_image_result = mysql.selectOne( """select * from """ + image_table_name + """ where id = %s""", (data['imageId'], )) if target_image_result[0] == 0: # 目标图片不存在 return self.replyMessage(message, state=False, msg="指定的图片没有找到") image = AnnotationProjectImage.create_by_database_result( target_image_result[1]) if image.annotationUserId != self.user.userId: # 当前用户没有锁定图片 return self.replyMessage(message, state=False, msg="请先标记此图片为要标注图片") inference_result = mysql.selectOne( """select * from AnnotationProjectInferencer where id = %s""", (data['inferencerId'], )) if not inference_result[0]: return self.replyMessage(message, state=False, msg="指定的标注器没有找到") inference_dict = mysql_dict_2_dict(inference_result[1]) if inference_dict["state"] != 'GENERAL': return self.replyMessage(message, state=False, msg="推理器正在工作,请等待推理完成后重试") script_path = inference_dict['script'] if not os.path.exists(script_path): return self.replyMessage(message, state=False, msg="指定的自动标注脚本没有找到") autoAnnotationLabelThread = AutoAnnotationLabelThread( ws=self.websocket, message=message, image_path=image.path, script_path=script_path, project_id=project.id, user_id=self.user.userId, inferencer_id=inference_dict["id"]) autoAnnotationLabelThread.start() finally: mysql.destory()
def handle(self, message): ''' @description: @param {type} @return: ''' logging.debug("'AnnotationMetaLabelHandler' receive message %s", message.to_json()) if message.data is None: return self.replyMessage(message, state=False, msg='50101') data = message.data if 'projectId' not in data or 'imageId' not in data or 'action' not in data: return self.replyMessage(message, state=False, msg="50102", data=None) if data['projectId'] != self.user.projectId: return self.replyMessage(message, state=False, msg="50103") context = TLPContext() project = context.get_project(self.user.projectId) mysql = MysqlManager() try: image_table_name = '`AnnotationProjectImage' + str( project.index) + '`' target_image_result = mysql.selectOne( """select * from """ + image_table_name + """ where id = %s""", (data['imageId'], )) if target_image_result[0] == 0: # 目标图片不存在 return self.replyMessage(message, state=False, msg="50104") image = AnnotationProjectImage.create_by_database_result( target_image_result[1]) if image.annotationUserId != self.user.userId: # 当前用户没有锁定图片 return self.replyMessage(message, state=False, msg="50105") action = data['action'] meta_label_table_name = '`AnnotationProjectImageMetaLabel' + str( project.index) + '`' if action == 'add': # 为图片增加新的meta标签 if 'labelId' not in data: # 要增加的label关联不存在 return self.replyMessage(message, state=False, msg="50106") labelId = data['labelId'] attribute = data['attribute'] if ( 'attribute' in data and data['attribute'] is not None) else None # 增加默认的置信度的设置 if attribute is not None: attribute = self._merge_default_attribute( context.get_default_attribute(), attribute) sql = """insert into """ + meta_label_table_name + """ (`id`, `imageId`, `labelId`, `type`, `version`, `attribute`, `userId`, `createTime`, `updateTime`) values (%s, %s, %s, %s, %s, %s, %s, %s, %s)""" now = datetime.datetime.today() newId = str(uuid.uuid4()) count = mysql.insertOne( sql, (newId, image.id, labelId, 'MANUAL', '0', attribute, self.user.userId, now, now)) return self.replyMessage(message, state=True, msg="add meta label success.", id=newId, imageId=image.id, labelId=labelId) # if count: # return self.replyMessage(message, state=True, msg="add meta label success.", id=newId, imageId=image.id, labelId=labelId) # else: # return self.replyMessage(message, state=False, msg="50107", imageId=image.id, labelId=labelId) # 插入为写入数据库 elif action == 'delete': # 为图片取消一个meta标签 if 'id' not in data: # 要删除的ID不存在 return self.replyMessage(message, state=False, msg="50107") delete_id = data['id'] sql = """delete from """ + meta_label_table_name + """ where id = %s""" count = mysql.delete(sql, (delete_id, )) return self.replyMessage(message, state=True, msg="delete meta label success.", id=delete_id, imageId=image.id) # if count: # return self.replyMessage(message, state=True, msg="delete meta label success.", id=delete_id, imageId=image.id) # else: # return self.replyMessage(message, state=False, msg="50108", id=delete_id, imageId=image.id) # 删除未成功执行 elif action == 'update': # 为图像的Meta修改属性 if 'id' not in data: # 要删除的ID不存在 return self.replyMessage(message, state=False, msg="50107") update_id = data['id'] attribute = data['attribute'] if ( 'attribute' in data and data['attribute'] is not None) else None sql = """update """ + meta_label_table_name + """ set attribute = %s where id = %s""" count = mysql.update(sql, (attribute, update_id)) return self.replyMessage( message, state=True, msg="update meta label attribute success.", id=update_id, imageId=image.id) # if count: # return self.replyMessage(message, state=True, msg="update meta label attribute success.", id=update_id, imageId=image.id) # else: # return self.replyMessage(message, state=False, msg="50108", id=update_id, imageId=image.id) # 删除未成功执行 else: # 未知的meta操作 return self.replyMessage(message, state=False, msg="50109", imageId=image.id) # 未知的Action操作目标 finally: mysql.destory()
def handle(self, message): ''' @description: @param {type} @return: ''' logging.debug("'ImagesListHandler' receive message %s", message.to_json()) if not hasattr(message, 'data') or message.data is None: return self.replyMessage(message, state=False, msg='30001') data = message.data if 'projectId' not in data: return self.replyMessage(message, state=False, msg="30002") if self.user.projectId != data['projectId']: return self.replyMessage(message, state=False, msg="30003") project_id = self.user.projectId mysql = MysqlManager() tlpConfig = Config() page_size = tlpConfig.page_size current_image_id = None try: current_user_id = self.user.userId context = TLPContext() current_project = context.get_project(self.user.projectId) project_index = current_project.index image_table_name = '`AnnotationProjectImage' + str(project_index) + '`' if "imageIds" not in data or not data["imageIds"]: sql = """SELECT COUNT(1) total FROM """ + image_table_name total_rows = mysql.selectOne(sql)[1]['total'] total_page = math.ceil(total_rows / page_size) current_page = 0 page = 0 if 'page' not in data: # 没有指定当前浏览的数据 current_image_result = mysql.selectAll("""SELECT * FROM """ + image_table_name + """ WHERE `annotationUserId` = %s or `reviewUserId` = %s or `browseUserId` = %s""", (current_user_id, current_user_id, current_user_id)) if (current_image_result[0] > 0): # 已经有在处理的数据,需要知道是第几页的数据,从而继续从那边继续 current_images = current_image_result[1] for image in current_images: # 除了是正在标注的,其他的位置随便 if current_image_id is None: current_image_id = image["id"] if image["annotationUserId"] is not None: current_image_id = image["id"] sql = """ SELECT rowNum FROM ( SELECT id, @ROWNUM := @ROWNUM + 1 AS rowNum FROM """ + image_table_name + """, (SELECT @ROWNUM := 0) r ORDER BY `name` ASC, `id` DESC ) t WHERE id = %s """ result = mysql.selectOne(sql, (current_image_id, )) current_image_number = result[1]['rowNum'] current_page = int(current_image_number / page_size) current_row = current_image_number % page_size if current_page == 0: # TODO:随机一个page数据 pass else: page = current_page else: # 没有在处理的数据,并且没有指定页数,也就是刚开始标注 current_page = 0 # TODO:随机一个page数据 else: # 已指定浏览信息,直接获取对应的要求数据 current_page = data['page'] action = data['action'] if action == 'next': page = current_page elif action == 'last': page = current_page - 2 # 是否要配置页面list的分页数量>分页量小访问频繁,分页量大数据获取慢 # 使用有优化效果的Join子句 sql = """SELECT i1.* FROM """ + image_table_name + """ AS i1 INNER JOIN (SELECT id FROM """ + image_table_name + """ ORDER BY `name` ASC, `id` DESC LIMIT %s, %s) i2 ON i1.id = i2.id""" page_result = mysql.selectAll(sql, ((page * page_size), page_size)) if not page_result[0]: return self.replyMessage(message, state=False, msg='当前项目还没有可标注的图片') image_list = [] for i in range(len(page_result[1])): image_list.append(AnnotationProjectImage.convert_database_result_2_dict(page_result[1][i])) # 组织返回的数据 # return_data = dict() # return_data['projectId'] = project_id # return_data['totalRow'] = total_rows # return_data['totalPage'] = total_page # return_data['page'] = page + 1 # return_data['pageSize'] = page_size # return_data['currentImageId'] = current_image_id return self.replyMessage(message, state=True, msg='', projectId=project_id, totalRow=total_rows, totalPage=total_page, page=(page + 1), pageSize=page_size, currentImageId=current_image_id.decode('utf-8') if current_image_id is not None else '', images=image_list ) else: imageIds = data["imageIds"] imageIds_str = "" for imageId in imageIds: imageIds_str += "'" + imageId + "'," imageIds_str = imageIds_str[:-1] sql = """select * from """ + image_table_name + """ where imageId in (""" + imageIds_str + """)""" page_result = mysql.selectAll(sql) if not page_result[0]: return self.replyMessage(message, state=False, msg='没有找到指定要标注的图片') image_list = [] for i in range(len(page_result[1])): image_list.append(AnnotationProjectImage.convert_database_result_2_dict(page_result[1][i])) return self.replyMessage(message, state=True, msg='', projectId=project_id, totalRow=page_result[0], totalPage=1, page=1, pageSize=page_result[0], currentImageId='', images=image_list ) finally: mysql.destory()
def handle(self, message): ''' @description: @param {type} @return: ''' logging.debug("'UnlocakImageHandler' receive message %s", message.to_json()) if message.data is None: return self.replyMessage(message, state=False, msg='30201', data=None) data = message.data if 'projectId' not in data or 'imageId' not in data or 'action' not in data: return self.replyMessage(message, state=False, msg="30202", data=None) if data['projectId'] != self.user.projectId: return self.replyMessage(message, state=False, msg="30203", data=None) context = TLPContext() project = context.get_project(self.user.projectId) mysql = MysqlManager() try: image_table_name = '`AnnotationProjectImage' + str( project.index) + '`' target_image_result = mysql.selectOne( """select * from """ + image_table_name + """ where id = %s""", (data['imageId'], )) if target_image_result[0] == 0: # 目标图片不存在 return self.replyMessage(message, state=False, msg="指定的图片不存在,请确认。", data=None) image = AnnotationProjectImage.create_by_database_result( target_image_result[1]) if image.annotationUserId != self.user.userId and image.reviewUserId != self.user.userId and image.completedUserId != self.user.userId: # 当前用户没有锁定图片 return self.replyMessage(message, state=False, msg="您还没有锁定这张图片,无权解锁。", data=None) action = data['action'] if action == 'annotation' and image.annotationUserId != self.user.userId: return self.replyMessage(message, state=False, msg="30206", data=None) if action == 'review' and image.annotationUserId != self.user.userId: return self.replyMessage(message, state=False, msg="30206", data=None) if action == 'completed' and image.annotationUserId != self.user.userId: return self.replyMessage(message, state=False, msg="30206", data=None) sql_start = """update """ + image_table_name + """ set """ sql_end = """ where `id` = %s and `updateVersion` = %s """ result = 0 if action == "annotation": result = mysql.update( sql_start + """`annotation` = 0, `annotationUserId` = null, `updateVersion` = `updateVersion` + 1 """ + sql_end, (image.id, image.updateVersion)) elif action == "review": result = mysql.update( sql_start + """`review` = 0, `reviewUserId` = null, `updateVersion` = `updateVersion` + 1 """ + sql_end, (image.id, image.updateVersion)) elif action == 'completed': result = mysql.update( sql_start + """`completed` = 0, `completedUserId` = null, `updateVersion` = `updateVersion` + 1 """ + sql_end, (image.id, image.updateVersion)) if not result: # 更新行数小于1,数据异常(id, userId等), 乐观锁 return self.replyMessage(message, state=False, msg="30206", data=None) notice_msg = self.replyMessage(message, state=True, msg="notice", type="unlock-image", action=action, userId=self.user.userId, projectId=project.id, imageId=image.id) notice_msg.senderMid = MessageMid.IMAGES() # 更新成功,要对多有当前已经连接到次项目的用户发出通知 context.notice(notice_msg, project.id) return None finally: mysql.destory()
def handle(self, message): ''' @description: @param {type} @return: ''' logging.debug("'ImageLabelsHandler' receive message %s", message.to_json()) if not hasattr(message, 'data') or message.data is None: return self.replyMessage(message, state=False, msg='请指定消息体') data = message.data if 'projectId' not in data: return self.replyMessage(message, state=False, msg="请指定要处理的项目") if self.user.projectId != data['projectId']: return self.replyMessage(message, state=False, msg="您无权处理此项目的数据") project_id = self.user.projectId if 'action' not in data: return self.replyMessage(message, state=False, msg="请指定操作类型") action = data['action'] mysql = MysqlManager() tlpConfig = Config() page_size = tlpConfig.page_size current_image_id = None try: current_user_id = self.user.userId context = TLPContext() current_project = context.get_project(self.user.projectId) project_index = current_project.index sql_start = """select * from `AnnotationProjectLabelTemplate` where projectId = %s """ sql_end = """ order by name asc""" if action == 'meta': meta_label_list = [] lable_result = mysql.selectAll( sql_start + """ and `type` = 'Meta' """ + sql_end, (project_id, )) if lable_result[0]: for result in lable_result[1]: meta_label_list.append( AnnotationProjectLabelTemplate. convert_database_result_2_dict(result)) return self.replyMessage(message, state=True, msg='', projectId=project_id, metaLabes=meta_label_list) elif action == 'region': region_label_list = [] lable_result = mysql.selectAll( sql_start + """ and `type` = 'REGION' """ + sql_end, (project_id, )) if lable_result[0]: for result in lable_result[1]: region_label_list.append( AnnotationProjectLabelTemplate. convert_database_result_2_dict(result)) return self.replyMessage(message, state=True, msg='', projectId=project_id, metaLabels=meta_label_list, regionLabels=region_label_list) else: meta_label_list = [] region_label_list = [] lable_result = mysql.selectAll(sql_start + sql_end, (project_id, )) if lable_result[0]: for result in lable_result[1]: if result["type"].decode("utf-8").lower() == 'meta': meta_label_list.append( AnnotationProjectLabelTemplate. convert_database_result_2_dict(result)) else: region_label_list.append( AnnotationProjectLabelTemplate. convert_database_result_2_dict(result)) return self.replyMessage(message, state=True, msg='', projectId=project_id, metaLabels=meta_label_list, regionLabels=region_label_list) finally: mysql.destory()
def handle(self, message): ''' @description: @param {type} @return: ''' logging.debug("'ListImageAnnotationInfoHandler' receive message %s", message.to_json()) if message.data is None: return self.replyMessage(message, state=False, msg='请填写需要提交的数据') data = message.data if 'projectId' not in data or 'imageId' not in data or 'action' not in data: return self.replyMessage(message, state=False, msg="请填写关键的信息", data=None) if data['projectId'] != self.user.projectId: return self.replyMessage(message, state=False, msg="您不属于当前项目,无法进行操作,请检查权限") context = TLPContext() project = context.get_project(self.user.projectId) project_index_str = str(project.index) mysql = MysqlManager() try: image_table_name = '`AnnotationProjectImage' + project_index_str + '`' target_image_result = mysql.selectOne( """select * from """ + image_table_name + """ where id = %s""", (data['imageId'], )) if target_image_result[0] == 0: # 目标图片不存在 return self.replyMessage(message, state=False, msg="要操作的图片没有找到") image = AnnotationProjectImage.create_by_database_result( target_image_result[1]) # if image.annotationUserId != self.user.userId: # # 当前用户没有锁定图片 # return self.replyMessage(message, state=False, msg="您并没有锁定要操作的图片,请确认信息") action = data['action'] meta_label_table_name = '`AnnotationProjectImageMetaLabel' + project_index_str + '`' image_region_table_name = '`AnnotationProjectImageRegion' + project_index_str + '`' image_region_label_table_name = '`AnnotationProjectImageRegionLabel' + project_index_str + '`' if action == 'image-meta-label': # 查询图片得meta信息 return self.replyMessage(message, state=True, msg="select meta lable success.", metaLabels=self.__select_meta_label( mysql, meta_label_table_name, image.id), action=action) elif action == 'image-region-label': # 查询图片得区域和区域得label信息 return self.replyMessage( message, state=True, msg="select region lable success.", regions=self.__select_region_and_region_label( mysql, image_region_table_name, image_region_label_table_name, image.id), action=action) elif action == 'region-label': # 查询单个形状得label信息 if 'regionId' not in data: return self.replyMessage(message, state=False, msg="region id not found.", regionLabels=[], action=action) regionId = data['regionId'] return self.replyMessage( message, state=True, msg="select region label success.", regionLabels=self.__select_one_region_label( mysql, image_region_label_table_name, regionId), action=action) elif action == 'all': # 查询所有信息 if context.get_label_filter_condition(id(self.websocket)): ( meta_label_list, region_list ) = self.__select_all_region_and_label_use_label_filter_condition( mysql, image.id, project_index_str, context.get_label_filter_condition(id(self.websocket))) return self.replyMessage(message, state=True, msg="select all label success.", metaLabels=meta_label_list, regionLabels=region_list, action=action) else: meta_label_list = self.__select_meta_label( mysql, meta_label_table_name, image.id) region_label_list = self.__select_region_and_region_label( mysql, image_region_table_name, image_region_label_table_name, image.id) return self.replyMessage(message, state=True, msg="select all label success.", metaLabels=meta_label_list, regionLabels=region_label_list, action=action) else: return self.replyMessage(message, state=False, msg="不明确得动作定义,请指定查询内容") except Exception as e: mysql.end(False) raise RunTimeException("get all regions code error.") finally: mysql.destory()
def handle(self, message): ''' @description: @param {type} @return: ''' logging.debug("'AnnotationRegionLabelHandler' receive message %s", message.to_json()) if message.data is None: return self.replyMessage(message, state=False, msg='40101') data = message.data if 'projectId' not in data or 'imageId' not in data or 'action' not in data: return self.replyMessage(message, state=False, msg="40102", data=None) if data['projectId'] != self.user.projectId: return self.replyMessage(message, state=False, msg="40103") context = TLPContext() project = context.get_project(self.user.projectId) mysql = MysqlManager() try: image_table_name = '`AnnotationProjectImage' + str( project.index) + '`' region_table_name = '`AnnotationProjectImageRegion' + str( project.index) + '`' region_label_table_name = '`AnnotationProjectImageRegionLabel' + str( project.index) + '`' target_image_result = mysql.selectOne( """select * from """ + image_table_name + """ where id = %s""", (data['imageId'], )) if target_image_result[0] == 0: # 目标图片不存在 return self.replyMessage(message, state=False, msg="40104") image = AnnotationProjectImage.create_by_database_result( target_image_result[1]) if image.annotationUserId != self.user.userId: # 当前用户没有锁定图片 return self.replyMessage(message, state=False, msg="40105") action = data['action'] userId = self.user.userId now = datetime.datetime.today() insert_region_sql = """insert into """ + region_table_name + """ (`id`, `imageId`, `type`, `index`, `shape`, `shapeData`, `userId`, `createTime`, `updateTime`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)""" insert_region_label_sql = """insert into """ + region_label_table_name + """(`id`, `imageId`, `regionId`, `labelId`, `type`, `version`, `attribute`, `userId`, `createTime`, `updateTime`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""" update_region_label_sql = """update """ + region_label_table_name + """ set attribute = %s, updateTime = %s where id = %s""" if action == 'save-all': # 更新全部的页面Region标签 if 'regions' not in data: return self.replyMessage(message, state=False, msg="40109") regions = data['regions'] (insert_region_list, insert_region_lable_list, update_region_list, update_region_label_list ) = self.__create_region_and_region_label_data( context, regions, image.id, userId, now) # if not insert_region_list and not update_region_list and not update_region_label_list: # return self.replyMessage(message, state=False, msg="40110") # 没有可以写入的数据 if insert_region_list: insert_region_data = [] for region_obj in insert_region_list: insert_region_data.append( region_obj.to_value_list( ('id', 'imageId', 'type', 'index', 'shape', 'shapeData', 'userId', 'createTime', 'updateTime'))) # 新的标注形状写入 mysql.close_transaction_insert_many( insert_region_sql, insert_region_data) if insert_region_lable_list: # 继续写入需要新的label数据 insert_region_label_data = [] for label_object in insert_region_lable_list: insert_region_label_data.append( label_object.to_value_list( ('id', 'imageId', 'regionId', 'labelId', 'type', 'version', 'attribute', 'userId', 'createTime', 'updateTime'))) # 新的标注属性 mysql.close_transaction_insert_many( insert_region_label_sql, insert_region_label_data) if update_region_list: # 如果有需要更新的矩形数据--更新 update_region_sql = """update """ + region_table_name + """ set shapeData = %s, updateTime = %s where id = %s""" for region_obj in update_region_list: mysql.update(update_region_sql, parameter=(region_obj.shapeData, region_obj.updateTime, region_obj.id), auto_commit=False) if update_region_label_list: # 如果有需要更新的矩形标签--更新 for label_object in update_region_label_list: mysql.update(update_region_label_sql, parameter=(label_object.attribute, label_object.updateTime, label_object.id), auto_commit=False) if 'deleteRegions' in data: # 删除对应的区域和属性 delete_region_sql = """delete from """ + region_table_name + """ where id = %s""" deleteRegions = data['deleteRegions'] for region_id in deleteRegions: mysql.delete(sql=delete_region_sql, parameter=(region_id, ), auto_commit=False) if 'deleteRegionLabels' in data: # 删除对应的属性 delete_region_label_sql = """delete from """ + region_label_table_name + """ where id = %s""" deleteRegionLabels = data['deleteRegionLabels'] for region_label_id in deleteRegionLabels: mysql.delete(sql=delete_region_label_sql, parameter=(region_label_id, ), auto_commit=False) # 都执行成功了,commit一次到数据库 mysql.end() get_all_region_sql = """select * from """ + region_table_name + """ where imageId = %s""" image_all_region_result = mysql.selectAll( get_all_region_sql, (image.id, )) image_all_region_label_result = [] result_data = [] if image_all_region_result[0]: # 有相关的数据,返回的时候返回当前数据的所有结构 get_all_region_label_where_sql = "" for region in image_all_region_result[1]: get_all_region_label_where_sql += "regionId = '" + region[ "id"].decode("utf-8") + "' or " get_all_region_label_where_sql = get_all_region_label_where_sql[ 0:-3] get_all_region_label_sql = """select * from """ + region_label_table_name + """ where """ + get_all_region_label_where_sql image_all_region_label_result = mysql.selectAll( get_all_region_label_sql) for region in image_all_region_result[1]: region_dict = AnnotationProjectImageRegion.convert_database_result_2_dict( region) region_dict["labels"] = [] if image_all_region_label_result[0]: for region_label in image_all_region_label_result[ 1]: if region_label["regionId"].decode( "utf-8") == region_dict['id']: region_dict["labels"].append( AnnotationProjectImageRegionLabel. convert_database_result_2_dict( region_label)) result_data.append(region_dict) return self.replyMessage( message, state=True, msg="save all region lable success.", imageId=image.id, regions=result_data, action="save-all") else: # 没有形状信息就直接返回一个图片的ID return self.replyMessage( message, state=True, msg="save all region lable success.", imageId=image.id, action="save-all") elif action == "save-one": if "regionId" not in data: return self.replyMessage(message, state=False, msg="请指定要标签的区域信息") region_id = data['regionId'] if "labelId" not in data or not data['labelId']: return self.replyMessage(message, state=False, msg="请指定要标签的模板信息") label_id = data['labelId'] lid = None if "id" in data and len(data['id']) > 0: lid = data["id"] attribute = None if "attribute" in data and data['attribute']: attribute = data["attribute"] # 增加默认的置信度的设置 if attribute is not None: attribute = self._merge_default_attribute( context.get_default_attribute(), attribute) if region_id == "": region_id = str(uuid.uuid4()) new_region = AnnotationProjectImageRegion( id=region_id, imageId=image.id, type='MANUAL', index=data['index'], shape=data['shape'], shapeData=data['shapeData'], userId=userId, createTime=now, updateTime=now) mysql.close_transaction_insert_many( insert_region_sql, (new_region.to_value_list( ('id', 'imageId', 'type', 'index', 'shape', 'shapeData', 'userId', 'createTime', 'updateTime')), )) label_obj = AnnotationProjectImageRegionLabel( id=lid, imageId=image.id, regionId=region_id, labelId=label_id, type="MANUAL", version="0", attribute=attribute, userId=userId, createTime=now, updateTime=now) if label_obj.id is None: # insert label_obj.id = str(uuid.uuid4()) mysql.close_transaction_insert_many( insert_region_label_sql, (label_obj.to_value_list( ('id', 'imageId', 'regionId', 'labelId', 'type', 'version', 'attribute', 'userId', 'createTime', 'updateTime')), )) else: # update mysql.update(sql=update_region_label_sql, parameter=(label_obj.attribute, now, label_obj.id), auto_commit=False) # 都执行成功了,commit一次到数据库 mysql.end() return self.replyMessage(message, state=True, msg="区域标签保存成功", action="save-one", labelInfo=label_obj.to_dict()) except Exception as e: mysql.end(False) raise RunTimeException("save all regions code error.") finally: mysql.destory()
def handle(self, message): ''' @description: @param {type} @return: ''' logging.debug("'LockImageHandler' receive message %s", message.to_json()) if message.data is None: return self.replyMessage(message, state=False, msg='30101') data = message.data if 'projectId' not in data or 'imageId' not in data or 'action' not in data: return self.replyMessage(message, state=False, msg="30102") if data['projectId'] != self.user.projectId: return self.replyMessage(message, state=False, msg="30103") context = TLPContext() project =context.get_project(self.user.projectId) mysql = MysqlManager() try: image_table_name = '`AnnotationProjectImage' + str(project.index) + '`' target_image_result = mysql.selectOne("""select * from """ + image_table_name + """ where id = %s""", (data['imageId'], )) if target_image_result[0] == 0: # 目标图片不存在 return self.replyMessage(message, state=False, msg="30104") target_image = target_image_result[1] if target_image['annotation'] or target_image['review'] or target_image['completed']: # 已经被锁住的图片,不管是否是自己锁得都不予继续处理 return self.replyMessage(message, state=False, msg="30105") action = data['action'] sql_start = """update """ + image_table_name + """ set """ sql_end = """ where `id` = %s and `updateVersion` = %s """ result = 0 if action == "annotation": select_result = mysql.selectOne("""select `id`, `updateVersion` from """ + image_table_name + """ where annotation = 1 and annotationUserId = %s""", (self.user.userId, )) if select_result[0]: unlock_image_id = select_result[1]['id'].decode("utf-8") update_version= select_result[1]['updateVersion'] result = mysql.update(sql_start + """ `annotation` = 0, `annotationUserId` = null, `updateVersion` = `updateVersion` + 1 """ + sql_end, (unlock_image_id, update_version)) if result > 0: notice_msg = self.replyMessage(message, state=True, msg="notice", type="unlock-image", action=action, userId=self.user.userId, projectId=project.id, imageId=unlock_image_id) notice_msg.senderMid = MessageMid.IMAGES() notice_msg.messageType = 'unlock-image' context.notice(notice_msg, project.id) # 发送自动解锁的通知 result = mysql.update(sql_start + """`annotation` = 1, `annotationUserId` = %s, `updateVersion` = `updateVersion` + 1 """ + sql_end, (self.user.userId, data['imageId'], target_image['updateVersion'])) else: result = mysql.update(sql_start + """`annotation` = 1, `annotationUserId` = %s, `updateVersion` = `updateVersion` + 1 """ + sql_end, (self.user.userId, data['imageId'], target_image['updateVersion'])) elif action == "review": select_result = mysql.selectOne("""select `id`, `updateVersion` from """ + image_table_name + """ where `review` = 1 and `reviewUserId` = %s""", (self.user.userId, )) if select_result[0]: unlock_image_id = select_result[1]['id'].decode("utf-8") update_version= select_result[1]['updateVersion'] result = mysql.update(sql_start + """ `review` = 0, `reviewUserId` = null, `updateVersion` = `updateVersion` + 1 """ + sql_end, (unlock_image_id, update_version)) if result > 0: notice_msg = self.replyMessage(message, state=True, msg="notice", type="unlock-image", action=action, userId=self.user.userId, projectId=project.id, imageId=unlock_image_id) notice_msg.senderMid = MessageMid.IMAGES() notice_msg.messageType = 'unlock-image' context.notice(notice_msg, project.id) # 发送自动解锁的通知 result = mysql.update(sql_start + """`review` = 1, `reviewUserId` = %s, `updateVersion` = `updateVersion` + 1 """ + sql_end, (self.user.userId, data['imageId'], target_image['updateVersion'])) else: result = mysql.update(sql_start + """`review` = 1, `reviewUserId` = %s, `updateVersion` = `updateVersion` + 1 """ + sql_end, (self.user.userId, data['imageId'], target_image['updateVersion'])) elif action == 'completed': result = mysql.update(sql_start + """`completed` = 1, `completedUserId` = %s, `completedTime` = %s, `updateVersion` = `updateVersion` + 1 """ + sql_end, (self.user.userId, datetime.datetime.today(), data['imageId'], target_image['updateVersion'])) if not result: # 更新行数小于1,数据异常(id, userId等), 乐观锁 return self.replyMessage(message, state=False, msg="操作出现冲突,请重试") notice_msg = self.replyMessage(message, state=True, msg="notice", type="lock-image", action=action, userId=self.user.userId, projectId=project.id, imageId=data["imageId"]) notice_msg.senderMid = MessageMid.IMAGES() # 更新成功,要对多有当前已经连接到次项目的用户发出通知 context.notice(notice_msg, project.id) return None finally: mysql.destory()