Пример #1
0
 def refresh(self):
     tic = time.perf_counter()
     User.objects(userId=self.userId).update(set__isFreshing=True, set__isSync=False)
     nPT = ''
     pool=ThreadPool(self.queue)
     params = {'pageSize': self.pageNum}
     i = 0
     try:
         if not os.path.isdir(f'{self.IFR}/{self.userId}'):
             os.mkdir(f'{self.IFR}/{self.userId}')
         while True:
             if nPT:
                 params['pageToken'] = nPT
             photoRes = self.session.get(
                 'https://photoslibrary.googleapis.com/v1/mediaItems', params=params).json()
             mediaItems = photoRes.get('mediaItems', None)
             if not mediaItems:
                 break
             print(f'Handling {len(mediaItems)} items')
             for mediaItem in mediaItems:
                 dbres = Photo.objects(photoId=mediaItem['id'])
                 mimeType, _ = mediaItem['mimeType'].split('/')
                 if not dbres and mimeType == 'image':
                     pool.add_task(self.color_pipline, mediaItem=mediaItem)
                     i=i+1
             if not os.getenv('CV_RELEASE', None) == "True" or not photoRes.get('nextPageToken', None):
                 break
             else:
                 nPT = photoRes['nextPageToken']
     except Exception as e:
         print(e)
     Thread(target=self.afterall, args=(tic,i), daemon=True).start()
Пример #2
0
def checkisSync(session, userId):
    params = {'pageSize': 10}
    photoRes = session.get(
        'https://photoslibrary.googleapis.com/v1/mediaItems',
        params=params).json()
    mediaItems = photoRes['mediaItems']
    print(f'Checking {len(mediaItems)} pics')
    for mediaItem in mediaItems:
        dbRes = Photo.objects(Q(userId=userId) & Q(photoId=mediaItem['id']))
        if not dbRes:
            print('some pic is missing')
            return False
    return True
Пример #3
0
 def color_pipline(self, mediaItem):
     try:
     # get the image data
         filename = mediaItem['filename']
         with open(f'{self.IFR}/{self.userId}/{filename}', mode='rb') as handler:
             image = Image(content = handler.read())
         objects = self.client.object_localization(image=image).localized_object_annotations
         result_array = color_detection(objects, f'{self.IFR}/{self.userId}/{filename}')
         for o, r in zip(objects, result_array):
             tempName = toSingleMan(o.name)
             name = tempName if tempName else o.name
             cm = ColorModel(obj=name)
             for i in r:
                 cm.color.append(i)
             Photo.objects(photoId=mediaItem['id']).update(push__tag__zh_tw__color=cm)
             cm = ColorModel(obj=o.name)
             for i in r:
                 cm.color.append(i)
             Photo.objects(photoId=mediaItem['id']).update(push__tag__en__color=cm)
     except Exception as e:
         print(f'Error from initial color api pipline {e}')
         print(traceback.format_exc())
Пример #4
0
 def update_location(self,locdata, userId):
     for l in locdata:
         lat, lng = l['location']['latitude'], l['location']['longitude']
         tempgeo = GeoData(latitude=lat,longitude=lng)
         temptime = datetime.datetime.fromtimestamp(l['timestamp'],tz=datetime.timezone.utc).replace(microsecond=0)
         reports = Photo.objects(Q(userId=userId) & Q(filename = l['filename']) & Q(createTime = temptime))
         print(reports)
         if reports:
             try:
                 toSave = self.reverse_geocoding(lat, lng, 'zh_TW', self.api_key)
                 for t in toSave:
                     results = reports.update(set__gps=tempgeo,add_to_set__tag__zh_tw__location=t)
                 toSave = self.reverse_geocoding(lat, lng, 'en', self.api_key)
                 for t in toSave:
                     results = reports.update(set__gps=tempgeo,add_to_set__tag__en__location=t)
                 print(results)
             except Exception as e:
                 print(e)
Пример #5
0
    def getpid(key):
        userid = '113073984862808105932'
        try:
            print('key', key)

            emo = Photo.objects(Q(userId=userid) & Q(tag__emotion_tag=key))
            print('emotion:', emo)
            addpid(emo, key)

            main = Photo.objects(Q(userId=userid) & Q(tag__main_tag=key))
            print('main:', main)
            addpid(main, key)

            top3 = Photo.objects(Q(userId=userid) & Q(tag__top3_tag__tag=key))
            print('top3:', top3)
            addpid(top3, key)

            alltag = Photo.objects(Q(userId=userid) & Q(tag__all_tag__tag=key))
            print('alltag:', alltag)
            addpid(alltag, key)

            custom = Photo.objects(
                Q(userId=userid) & Q(tag__custom_tag__is_deleted=False)
                & Q(tag__custom_tag__tag=key))
            print('custom:', custom)
            addpid(custom, key)

            location = Photo.objects(Q(userId=userid) & Q(location=key))
            print('location:', location)
            addpid(location, key)

            album = Album.objects(
                Q(userId=userid) & Q(albumTag__isDeleted=False)
                & Q(albumTag__tag=key))
            # print('album:',album)
            for i in album:
                photos = i.albumPhoto
                # print('photos:',photos)
                for j in photos:
                    atag = []
                    isdeleted = j.isDeleted
                    if isdeleted is False:
                        pid.append(j.photoId)
                        atag.append(key)
                        atemptag = atag
                        pid_tag.append({"pid": j.photoId, "tag": atemptag})
                        print('pid_tag', pid_tag)

        except Exception as e:
            print(e)
Пример #6
0
def get_url(res):
    parameters = res.query_result.parameters
    # print("parameters:",parameters)
    emotion = parameters.fields['emotion'].list_value
    date = parameters.fields['date'].list_value
    dateperiod = parameters.fields['date-period'].list_value
    vision = parameters.fields['visionAPI_1000'].list_value
    location = parameters.fields['location'].list_value
    pid = []
    pid_tag = []

    def addpid(pidarr, key):
        for i in pidarr:
            tag = []
            photoid = i.photoId
            # print('photoid:',photoid)
            pid.append(photoid)
            # print('key in add:',key)
            tag.append(key)
            temptag = tag
            # print('tag:',tag)
            pid_tag.append({"pid": photoid, "tag": temptag})
            print('pid_tag', pid_tag)

    def getpid(key):
        userid = '113073984862808105932'
        try:
            print('key', key)

            emo = Photo.objects(Q(userId=userid) & Q(tag__emotion_tag=key))
            print('emotion:', emo)
            addpid(emo, key)

            main = Photo.objects(Q(userId=userid) & Q(tag__main_tag=key))
            print('main:', main)
            addpid(main, key)

            top3 = Photo.objects(Q(userId=userid) & Q(tag__top3_tag__tag=key))
            print('top3:', top3)
            addpid(top3, key)

            alltag = Photo.objects(Q(userId=userid) & Q(tag__all_tag__tag=key))
            print('alltag:', alltag)
            addpid(alltag, key)

            custom = Photo.objects(
                Q(userId=userid) & Q(tag__custom_tag__is_deleted=False)
                & Q(tag__custom_tag__tag=key))
            print('custom:', custom)
            addpid(custom, key)

            location = Photo.objects(Q(userId=userid) & Q(location=key))
            print('location:', location)
            addpid(location, key)

            album = Album.objects(
                Q(userId=userid) & Q(albumTag__isDeleted=False)
                & Q(albumTag__tag=key))
            # print('album:',album)
            for i in album:
                photos = i.albumPhoto
                # print('photos:',photos)
                for j in photos:
                    atag = []
                    isdeleted = j.isDeleted
                    if isdeleted is False:
                        pid.append(j.photoId)
                        atag.append(key)
                        atemptag = atag
                        pid_tag.append({"pid": j.photoId, "tag": atemptag})
                        print('pid_tag', pid_tag)

        except Exception as e:
            print(e)

    if len(emotion) is not 0:
        emokey = emotion.values[0].string_value
        getpid(emokey)

    # 抓單一日期(ex:昨天)
    if len(date) is not 0:
        datekey = date.values[0].string_value
        datekey = datetime.strptime(datekey, "%Y-%m-%dT%H:%M:%S+08:00")
        datekey = datetime.strftime(datekey, "%Y-%m-%d")
        today = datetime.strptime(datekey, "%Y-%m-%d")
        # print('today',today)
        tomorrow = today + timedelta(days=1)
        tomorrow = datetime.strftime(tomorrow, "%Y-%m-%d")
        # print('tomorrow',tomorrow)
        date = Photo.objects(
            Q(userId=userid) & Q(createTime__lt=tomorrow)
            & Q(createTime__gt=datekey))
        print('date:', date)
        addpid(date, datekey)

    # 抓時間區間(ex:今年,上禮拜)
    if len(dateperiod) is not 0:
        dpstart = dateperiod.values[0].struct_value.fields[
            'startDate'].string_value
        dpend = dateperiod.values[0].struct_value.fields[
            'endDate'].string_value
        # print('dpstart',dpstart)
        # print('dpend',dpend)
        start = datetime.strptime(dpstart, "%Y-%m-%dT%H:%M:%S+08:00")
        start = datetime.strftime(start, "%Y-%m-%d")
        # print('start',start)
        end = datetime.strptime(dpend, "%Y-%m-%dT%H:%M:%S+08:00")
        end = datetime.strftime(end, "%Y-%m-%d")
        # print('end',end)

        dateperiod = Photo.objects(
            Q(userId=userid) & Q(createTime__lt=end) & Q(createTime__gt=start))
        print('dateperiod:', dateperiod)
        periodkey = start + '-' + end
        # print('periodkey:',periodkey)
        addpid(dateperiod, periodkey)

    if len(vision) is not 0:
        vikeyArray = map(lambda k: k.string_value, vision.values)
        vikeyArray = set(vikeyArray)
        vikeyArray = list(vikeyArray)
        for i in vikeyArray:
            getpid(i)

    if len(location) is not 0:
        admin_areakey = location.values[0].struct_value.fields[
            'admin-area'].string_value
        if (admin_areakey != ''):
            # print(admin_areakey)
            getpid(admin_areakey)
        bus_namekey = location.values[0].struct_value.fields[
            'business-name'].string_value
        if (bus_namekey != ''):
            # print(bus_namekey)
            getpid(bus_namekey)
        citykey = location.values[0].struct_value.fields['city'].string_value
        if (citykey != ''):
            # print(citykey)
            getpid(citykey)
        countrykey = location.values[0].struct_value.fields[
            'country'].string_value
        if (countrykey != ''):
            # print(countrykey)
            getpid(countrykey)
        islandkey = location.values[0].struct_value.fields[
            'island'].string_value
        if (islandkey != ''):
            # print(islandkey)
            getpid(islandkey)
        shortcutkey = location.values[0].struct_value.fields[
            'shortcut'].string_value
        if (shortcutkey != ''):
            # print(shortcutkey)
            getpid(shortcutkey)
        street_addresskey = location.values[0].struct_value.fields[
            'street-address'].string_value
        if (street_addresskey != ''):
            # print(street_addresskey)
            getpid(street_addresskey)
        subadmin_areakey = location.values[0].struct_value.fields[
            'subadmin-area'].string_value
        if (subadmin_areakey != ''):
            # print(subadmin_areakey)
            getpid(subadmin_areakey)
        zip_codekey = location.values[0].struct_value.fields[
            'zip-code'].string_value
        if (zip_codekey != ''):
            # print(zip_codekey)
            getpid(zip_codekey)
    print("pid:", pid)
    return pid
Пример #7
0
    def people_pipline(self, mediaItem):
        try:
            # get the image data
            filename = mediaItem['filename']
            addr = 'http://40.83.112.73:5239/'
            test_url = addr + '/api/yolov4/people'

            # prepare headers for http request
            content_type = 'image/jpeg'
            headers = {'content-type': content_type}

            img = cv2.imread(f'{self.IFR}/{self.userId}/{filename}')
            # encode image as jpeg
            _, img_encoded = cv2.imencode('.jpg', img)
            # send http request with image and receive response
            response = requests.post(test_url,
                                     data=img_encoded.tobytes(),
                                     headers=headers)
            # decode response
            result = json.loads(response.text)
            pt = PeopleTag(count=int(result['people']['count']))
            for o in result['people']['ontology']:
                pt.ontology.append(o)
            p = Photo.objects(photoId=mediaItem['id']).get()
            p.tag.zh_tw.people = pt
            pt = PeopleTag(count=result['people']['count'])
            for o in result['people']['ontology_en']:
                pt.ontology.append(o)
            p.tag.en.people = pt
            p.save()
            # image = Image.open(f'{self.IFR}/{self.userId}/{filename}')
            # image = np.asarray(image.resize((self.input_size, self.input_size)))
            # image = image / 255.
            # image = np.concatenate([image[np.newaxis, :, :]] * self.batch_size)
            # body = json.dumps({"instances": image.tolist()})
            # ep = Endpoint()
            # result = ep.main(body)
            # result = json.loads(result["Body"].read().decode("utf-8"))

            # value = tf.convert_to_tensor(result['predictions'], dtype=tf.float32)
            # boxes = value[:, :, 0:4]
            # pred_conf = value[:, :, 4:]
            # boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
            #     boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
            #     scores=tf.reshape(
            #         pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
            #     max_output_size_per_class=50,
            #     max_total_size=50,
            #     iou_threshold=0.45,
            #     score_threshold=0.25
            # )
            # pred_bbox = [boxes.numpy(), scores.numpy(), classes.numpy(), valid_detections.numpy()]
            # image_result, people_num, people_onto, people_onto_en = draw_bbox(pred_bbox)
            # pt_zh = PeopleTag(count=people_num, ontology=people_onto)
            # pt_en = PeopleTag(count=people_num, ontology=people_onto_en)
            # p = Photo.objects(photoId=mediaItem['id']).get()
            # p.tag.zh_tw.people = pt_zh
            # p.tag.en.people = pt_en
            # p.save()
        except Exception as e:
            print(f'Error from initial people api pipline {e}')
            print(traceback.format_exc())