def caculator(): date = request.args.get("date") image = request.args.get("image") if (date is None): return Response('[date] parameter is missing', status=403) if (image is None): return Response('[image] parameter is missing', status=403) print 'call date -> ', date, ' image -> ', image # 声明特征返回值 result = None try: time_start = time.time() # 数据库查找图片 item = mongodb.db(date).imagesource.find_one({'name': image}) time_db_find = time.time() if (item is None): return Response(image + ' is missing', status=404) # 写临时u图片到磁盘 imagepath = 'temp/' + item['name'] file = open(imagepath, 'wb') file.write(item['source']) file.close() time_file_write = time.time() # 计算特征值 app_conn.send(imagepath) time_vehicle_detect = time.time() result = app_conn.recv() # 删除临时图片 os.remove(imagepath) # 修改图片处理状态 mongodb.db(date).imagesource.update({'name': image}, {'$set': { 'state': 2 }}) except (ex1): return Response(str(ex1), status=500) else: print "db_find: ", (time_db_find - time_start) * 1000, "ms", "*********" print "file_write: ", (time_file_write - time_start) * 1000, "ms", "*********", ( time_file_write - time_db_find) * 1000 print "vehicle_detect: ", ( time_vehicle_detect - time_start) * 1000, "ms", "*********", ( time_vehicle_detect - time_file_write) * 1000 print result return Response(json.dumps(result, cls=ComplexEncoder), mimetype='application/json')
def Find(self, id): images = mongodb.db().image item = images.find_one({"_id": ObjectId(id)}) filesname = "/Users/ycysvler/Downloads/111-111.png" datatmp = open(filesname, 'wb') datatmp.write(item['source']) datatmp.close() print('complete')
def robotToUser(gender=1): print '\033[0;30;44m', '从机器人表导入用户,性别:', gender, '\033[0m' avatars = list(mongodb.db('robot').avatar.find({"gender": gender})) robots = list( mongodb.db('robot').robot.find({ "gender": gender, "userId": None })) count = len(robots) # 一共有多少个机器人 i = 0 while i < len(robots): # 机器人数据 robot = robots[i] # 用户数据 user = {} if i < len(avatars): user['avatar'] = avatars[i]['path'] user['fullName'] = robot['fullName'] user['gender'] = robot['gender'] user['favoriteCount'] = 0 user['createTime'] = datetime.datetime.utcnow() # 插入用户数据 u = mongodb.db('vision').user.insert(user) print str(i) + '/' + str(count), '\t', 'ID:', u, '\t', 'gender:', user[ 'gender'], '\t', 'createTime:', user[ 'createTime'], '\t', 'fullName:', user['fullName'] # 将用户ID回写到机器人表 mongodb.db('robot').robot.update({"_id": ObjectId(robot["_id"])}, {"$set": { "userId": u }}) i = i + 1 print ' 本次导入机器人:', i
def robotToKol(portalId): robots = list(mongodb.db('robot').robot.find({"userId": {"$ne": None}})) count = len(robots) # 一共有多少个机器人 print '\033[0;30;44m', '从机器人表导入kol:', portalId, count, '\033[0m' i = 0 while i < len(robots): # 机器人数据 robot = robots[i] # 用户数据 user = {} user['userId'] = robot['userId'] user['portalId'] = ObjectId(portalId) user['createTime'] = datetime.datetime.utcnow() # 插入用户数据 u = mongodb.db('vision').portal_user.insert(user) print str(i) + '/' + str( count), '\t', 'ID:', u, '\t', 'createTime:', user['createTime'] i = i + 1 print ' 本次导入KOL:', i
def initRobot(base, gender=1): f = open(base, "r") # 设置文件对象 data = f.readlines() # 直接将文件中按行读到list里,效果与方法2一样 f.close() # 关闭文件 robots = mongodb.db('robot').robot i = 1 for name in data: item = {} item['fullName'] = name.replace('\n', '') item['gender'] = gender item['portalId'] = None item['userId'] = None item['avatar'] = None robots.insert(item) print i, '\t', item['fullName'] i = i + 1 return None
def initAvatar(base, gender=1): images = os.listdir(base) count = len(images) i = 1 avatars = mongodb.db('robot').avatar # 遍历文件目录 for name in images: filename = os.path.join(base, name) if os.path.isfile(filename): # 获取七牛地址 path = qiniuUploadImage(filename) item = {} item['path'] = path item['gender'] = gender item['name'] = name avatars.insert(item) print ' ' + str(i) + '/' + str(count), '\t', path, '\t', name i = i + 1
def adapterAnalysis(dbdate, snaptime, name, kakouid, vehicle ): analysis = mongodb.db(dbdate).analysis #print vehicle item = {} item['name'] = name item['kakouid'] = kakouid item['vehiclezone'] = { "x": vehicle['vehicleZone'][0], "y": vehicle['vehicleZone'][1], "width": vehicle['vehicleZone'][2] - vehicle['vehicleZone'][0], "height": vehicle['vehicleZone'][3] - vehicle['vehicleZone'][1] } UTC_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ" utc = dbdate[0:4] + '-' + dbdate[4:6] + '-' + dbdate[6:8] + 'T' + '12:00:00.000Z' dt = datetime.datetime.strptime(utc, UTC_FORMAT) item['date'] = dt item['snaptime'] = item['date'] item['platehasno'] = 0 item['platetype'] = '' item['platenumber'] = '' if 'vehiclePlateLicense' in vehicle: if vehicle["vehiclePlateLicense"] <> None: item['platecolor'] = vehicle['vehiclePlateLicense']['color'] item['platenumber'] = vehicle['vehiclePlateLicense']['license'] item['platehasno'] = 1 # 如果没能计算出车型,就返回 if not ('vehicleType' in vehicle) or vehicle["vehicleType"] == None: logger.warning({"content":'vehicletype is None > %s %s %s'%(dbdate, kakouid, name)}) return vehicleTypes = vehicle['vehicleType']['category'].split('_') item['vehiclecarclass'] = vehicleTypes[0] item['vehicletype'] = vehicleTypes[1] item['vehiclebrand'] = vehicleTypes[2] item['vehiclemaker'] = vehicleTypes[3] item['vehiclemodel'] = vehicleTypes[4] item['vehicleyear'] = vehicleTypes[5] item['vehiclecolor'] = vehicle['vehicleColor']['category'] item['vehiclescore'] = round(float(vehicle['vehicleType']['score']),4) item['vehicleposture'] = 0 if vehicle["vehiclePosture"]['category'] == "车头" else 1 if 'vehicleStruct' in vehicle and vehicle["vehicleStruct"] <> None: item['withFrontWindowLabelInspection'] =1 if vehicle["vehicleStruct"]["withFrontWindowLabelInspection"] else 0 item['withFrontWindowAccessories'] = 1 if vehicle["vehicleStruct"]["withFrontWindowAccessories"] else 0 item['isTaxi'] = 1 if vehicle["vehicleStruct"]["isTaxi"] else 0 item['withDriverSafetyBelt'] = 1 if vehicle["vehicleStruct"]["withDriverSafetyBelt"] else 0 item['withSideSafetyBelt'] = 1 if vehicle["vehicleStruct"]["withSideSafetyBelt"] else 0 item['withCellPhone'] = 1 if vehicle["vehicleStruct"]["withCellPhone"] else 0 item['withFrontWindowObjects'] = 1 if vehicle["vehicleStruct"]["withFrontWindowObjects"] else 0 item['withOtherPeopleOnSideSeat'] = 1 if vehicle["vehicleStruct"]["withOtherPeopleOnSideSeat"] else 0 item['withSunShieldDown'] = 1 if vehicle["vehicleStruct"]["withSunShieldDown"] else 0 item['withSkyRoof'] = 1 if vehicle["vehicleStruct"]["withSkyRoof"] else 0 if 'driveSeatZone' in vehicle["vehicleStruct"]: item['driverrdsSeatZone'] = { "x": vehicle["vehicleStruct"]["driveSeatZone"][0], "y": vehicle["vehicleStruct"]["driveSeatZone"][1], "width": vehicle["vehicleStruct"]["driveSeatZone"][2] - vehicle["vehicleStruct"]["driveSeatZone"][0], "height": vehicle["vehicleStruct"]["driveSeatZone"][3] - vehicle["vehicleStruct"]["driveSeatZone"][1], "score": vehicle["vehicleStruct"]["driveSeatZone"][4], } if 'skyRoof' in vehicle["vehicleStruct"]: item['skyRoof'] = { "x": vehicle["vehicleStruct"]["skyRoof"][0], "y": vehicle["vehicleStruct"]["skyRoof"][1], "width": vehicle["vehicleStruct"]["skyRoof"][2] - vehicle["vehicleStruct"]["skyRoof"][0], "height": vehicle["vehicleStruct"]["skyRoof"][3] - vehicle["vehicleStruct"]["skyRoof"][1], "score": vehicle["vehicleStruct"]["skyRoof"][4], } analysis.insert(item) # 发送分析通知,供大数据分析 data = {} # data["platenumber"] = urlencode(item["platenumber"]) # data["platecolor"] = urlencode(item["platecolor"]) # data["platetype"] = urlencode(item["platetype"]) # data["vehiclebrand"] = urlencode(item["vehiclebrand"]) # data["vehiclemodel"] = urlencode(item["vehiclemodel"]) # data["vehicleyear"] = urlencode(item["vehicleyear"]) # data["vehiclemaker"] = urlencode(item["vehiclemaker"]) # data["vehiclecolor"] = urlencode(item["vehiclecolor"]) # data["vehicletype"] = urlencode(item["vehicletype"]) # data["vehiclescore"] = urlencode(item["vehiclescore"]) analitem = {} analitem['image'] = item['name'] analitem['carlabel'] = 0 analitem['brand'] = item['vehiclebrand'] analitem['firms'] = item['vehiclemaker'] analitem['model'] = item['vehiclemodel'] analitem['version'] = item['vehicleyear'] analitem['carconfidence'] = item['vehiclescore'] analitem['colortype'] = item['vehiclecolor'] analitem['colorlabel'] = 0 analitem['colorconfidence'] = 1 analitem['date'] = dbdate analitem['carclass'] = item['vehiclecarclass'] analitem['vehicletype'] = item['vehicletype'] analitem['platenumber'] = item['platenumber'] mysql.image_analytical(analitem)
# 格式化成2016-03-20 11:45:39形式 parser = argparse.ArgumentParser() date = getYesterday() #date = '20170427' parser.add_argument('--date', type=str, default = date ) args = parser.parse_args() date = args.date while True: logger.info({"content":'---------------- [ %s ] ----------------'%(date)}) imagesources = mongodb.db(date).imagesource.find({'state':0}) if os.path.isdir( 'temp' ) == False: os.makedirs( 'temp' ) for item in imagesources: logger.info({"content":'mongodb change state > %s %s'%(date, item['name'])}) mongodb.db(date).imagesource.update({'name':item['name']},{'$set':{'state':1}}) logger.info({"content":'calculation > %s %s'%(date, item['name'])}) start = time.time() imagepath = 'temp/' + item['name'] file = open(imagepath, 'wb') file.write(item['source']) file.close() # 计算特征值 im = cv2.imread(imagepath)
def clearUserByRobot(): print '\033[0;30;44m', '清除机器人', '\033[0m' robots = list(mongodb.db('robot').robot.find({"userId": {"$ne": None}})) count = len(robots) # 一共有多少个机器人 i = 1 for robot in robots: userId = robot['userId'] if userId != None: # 删用户 mongodb.db('vision').user.remove({"_id": userId}) # 清理关注关系 mongodb.db('vision').user_follow.remove({"userId": userId}) mongodb.db('vision').user_follow.remove({"follow": userId}) mongodb.db('vision').portal_user.remove({"userId": userId}) mongodb.db('vision').geo_favorite.remove({"follow": userId}) mongodb.db('vision').video.remove({"userId": userId}) print str(i) + '/' + str( count ), '\t', 'ID:', userId, '\t', 'fullName:', robot['fullName'] # 将用户ID回写到机器人表 mongodb.db('robot').robot.update_one( {"_id": ObjectId(robot["_id"])}, {"$set": { "userId": None }}) i = i + 1
def __init__(self): self.db = mongodb.db()
def __init__(self, host, date=time.strftime("%Y%m%d", time.localtime())): self.host = host self.port = '21' self.date = date self.imagesource = mongodb.db(self.date).imagesource self.ftp.connect( self.host, self.port )