def upload_photo(request): if request.method == 'POST': sim_file= request.FILES['sim_id'] f=sim_file.read() sim_id=int(f) #test if Sim with number=sim_id exists if Sim.objects.filter(number = sim_id).count()==1: #Creating Photo object uploaded_photo = Photo(phototype= 'Blank', image = request.FILES['file_upload']) #Creating Notification object and save to DBB mySim=Sim.objects.get(number = sim_id) myBox=Box.objects.get(sim = mySim) if Notification.objects.filter(title__contains = uploaded_photo.image).count() > 1: i=0 for notification in Notification.objects.filter(title__contains = uploaded_photo.image): i+=1 titleu=str(uploaded_photo.image) + str(i) myNotification = Notification(title = titleu, box=myBox) else: myNotification = Notification(title = uploaded_photo.image, box=myBox) #Traitement d'image myNotification.save() #Polishing Photo object and save to DBB uploaded_photo.notification=myNotification uploaded_photo.save() #to remove str(sim_id) return HttpResponse("Image Uploaded, owner of sim_id = " + str(sim_id)) else: return HttpResponse("ACCESS DENIED: Box Not Identified") else: return HttpResponse("GET Denied")
def upload(request): attachedPhotoFiles = [(fieldName, file) for fieldName, file in request.FILES.iteritems() if 'photo' in fieldName] ids = [] for fieldName, file in attachedPhotoFiles: photo = Photo(photo=file) photo.save() ids.append(photo.id) return HttpResponse(json.dumps(ids, sort_keys=True, indent=2), content_type="application/json")
def scrape(self): from photo.models import Photo img_url = 'https://graph.facebook.com/%s/picture' % self.fbid try: Photo.create_from_url(img_url, facebook=self) self.scraped = True self.save() except: print "some issue with %s" % img_url
def test_read_exif(self): """ 测试exif读取,需要准备一张含有exif的照片 :return: None """ photo = Photo(path="/IMG_20181202_084309.jpg") photo._read_exif() print("datetime_original:", photo.datetime_original) print("gps:", photo.gps_longitudes, photo.gps_latitudes) self.assertIsNotNone(photo.datetime_original) self.assertIsNotNone(photo.gps_longitudes) self.assertIsNotNone(photo.addr_text)
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()
def photo_load(request, pk, apk): o = get_object_or_404(Object, pk=pk) a = get_object_or_404(Album, pk=apk) if request.method == 'POST': for file in request.FILES.getlist('file'): p = Photo() p.owner = request.user p.published_date = timezone.now() p.album = a p.photo.save(str(file), file, save=True) p.thumb_make() p.save() return redirect('object.views.object_view', pk=o.pk)
def pipeline(self, mediaItem): # only download images try: # get the image data filename = mediaItem['filename'] imagebinary = self.session.get(mediaItem['baseUrl'] + '=d').content with open(f'{self.IFR}/{self.userId}/{filename}', mode='wb') as handler: handler.write(imagebinary) image = Image(content=imagebinary) response = self.client.label_detection(image=image) if response.error.message: raise Exception(response.error.message) labels = response.label_annotations ltemp = list(map(getLabelDescription, labels)) mLabels = toMandarin(ltemp) t = Tag() bs = BasicStructure() for el, l in zip(ltemp, labels): bs.main_tag.append(ATag(tag=el, precision=l.score)) t.en = bs if mLabels and len(mLabels) > 0: bs = BasicStructure() for ml, l in zip(mLabels, labels): bs.main_tag.append(ATag(tag=ml, precision=l.score)) t.zh_tw = bs tempcreationTime = mediaItem['mediaMetadata']['creationTime'] sliceTime = tempcreationTime.split('Z')[0].split( '.')[0] if '.' in tempcreationTime else tempcreationTime.split( 'Z')[0] realTime = datetime.datetime.strptime(sliceTime, "%Y-%m-%dT%H:%M:%S") pho = Photo( photoId=mediaItem['id'], filename=filename, userId=self.userId, tag=t, createTime=make_aware(realTime, timezone=pytz.timezone( settings.TIME_ZONE)), ) pho.save() except Exception as e: logging.error(e) print(f'Error from initial vision api pipline {e}') print(traceback.format_exc())
def thread(request, title): if request.method == 'POST': # webhook POST receive action params = request.POST try: # titleなスレがなかったらエラーがおきるはず thread = Thread.objects.filter(title=title).get() try: # shooting_atがなかったら現在時刻 shooting_at = datetime.strptime(params['shooting_at'], '%Y/%m/%d %H:%M:%S') except: shooting_at = datetime.now() # passphrase確認 if thread.phrase == params['pass']: photo = Photo(name=params['photo_id'], shooting_at=shooting_at, thread=thread) photo.image.save('%s.jpg' % params['photo_id'], request.FILES['imagedata']) photo.save() thread.modified_at = datetime.now() thread.save() finally: # エラーや結果に関係なくレスポンスは返す return HttpResponse('hi') if request.method == 'GET': try: # titleスレがすでにある場合は写真一覧 thread = Thread.objects.filter(title=title).get() photos = thread.photo_set.order_by('-created_at')[:40] thread.photos = [] for photo in photos: thread.photos.append(photo) ec = { 'thread' : thread } return direct_to_template(request, 'photo/thread.html', extra_context=ec) except: thread = Thread(title=title, phrase="") ec = { 'thread' : thread } # スレがない場合はパスフレーズ入力フォーム return direct_to_template(request, 'photo/thread.html', extra_context=ec)
def photo_load (request, pk, apk): o = get_object_or_404(Object, pk=pk) a = get_object_or_404(Album, pk=apk) if request.method == 'POST': for file in request.FILES.getlist('file'): p = Photo() p.owner = request.user p.published_date = timezone.now() p.album = a p.photo.save(str(file),file,save=True) p.thumb_make() p.save() return redirect('object.views.object_view', pk=o.pk)
def scrape(self): from photo.models import Photo o = OkCupidAccount.objects.all()[0] o.login() agent('Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16') url = 'http://www.okcupid.com/profile/%s/photos?cf=regular_indirect' % self.okid go(url) html = show() b = BeautifulSoup(html) divs = b.findAll('div', {'class': 'pic clearfix'}) for div in divs: img_tag = div.find('img') try: Photo.create_from_url(url=img_tag['src'], okcupid=self) except: print "no img found" self.scraped = True self.save()
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
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())
def post(self, request): create_time = request.POST.get('create_time', '1') photo_name = request.POST.get('photo_name', '1') photo = Photo() photo.create_time = create_time photo.image = photo_name photo.save() print(create_time, photo_name) return JsonResponse({"res": 1, "message": "chenggong"})
def upload_test(request): if request.method == 'POST': sim_id= request.POST['sim_id'] #test if Sim with number=sim_id exists if Sim.objects.filter(number = sim_id).count()==1: #Creating Photo object uploaded_photo = Photo(phototype= 'Blank', image = request.FILES['file_upload']) #Creating Notification object and save to DBB mySim=Sim.objects.get(number = sim_id) myBox=Box.objects.get(sim = mySim) if Notification.objects.filter(title__contains = uploaded_photo.image).count() > 1: i=0 for notification in Notification.objects.filter(title__contains = uploaded_photo.image): i+=1 titleu=str(uploaded_photo.image) + str(i) myNotification = Notification(title = titleu, box=myBox) else: myNotification = Notification(title = uploaded_photo.image, box=myBox) #Traitement d'image myNotification.save() #Polishing Photo object and save to DBB uploaded_photo.notification=myNotification uploaded_photo.save() #C:\Users\Elomario\Desktop\PI\Phase2\CMAIL-django\ #notificationu=Notification.objects.get(title='test recette') #photou=Photo.objects.get(notification=notificationu) print(str(uploaded_photo.image)) ls_fd = subprocess.Popen('static\SimpleColorDetection_TEST.exe ' + str(uploaded_photo.image),stdout=subprocess.PIPE).communicate()[0] print('done') out = ls_fd #ENDOFIMAGEHANDLING if "letter" in str(out): uploaded_photo.phototype='enveloppe' elif "colis" in str(out): uploaded_photo.phototype='colis' elif "pub" in str(out): uploaded_photo.phototype='pub' uploaded_photo.save() return HttpResponse("DO A POST " + str(out)) else: return HttpResponse("ACCESS DENIED: Box Not Identified")
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)
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)
def photo_save(request: HttpRequest, auction_id: int): if not request.user.is_authenticated: return JsonResponse({'result': 0, 'msg': 'Вы не авторизованы'}) try: auction = Auction.objects.get(pk=auction_id) except Auction.DoesNotExist: return JsonResponse({'result': 0, 'msg': 'Аукцион не существует'}) if auction.user.id != request.user.id: return JsonResponse({'result': 0, 'msg': 'Ошибка загрузки файла'}) photo = Photo(auction_id=auction_id) form = PhotoForm(request.POST, request.FILES, instance=photo) if form.is_valid(): try: form.save() except IntegrityError: return JsonResponse({'result': 0, 'msg': 'Ошибка загрузки файла'}) else: return JsonResponse({'result': 0, 'msg': 'Ошибка загрузки файла'}) request.session['open_gallery_tab'] = True return JsonResponse({'result': 1})
def photo_handle(request): userFolder = "img/" + str(request.user.id) + "-" + \ str(request.user.username) ffolder = '{}/{}'.format(settings.MEDIA_ROOT, userFolder) if not os.path.exists(ffolder): os.makedirs(ffolder) f1 = request.FILES.get('pic') # 从前端获取上传的图片 fname = ffolder + "/" + f1.name # 图片的完整路径 # print(fname) with open(fname, 'wb') as pic: # 文件操作 for c in f1.chunks(): # 因为图片存储的方式是二进制流,用f1.chunks()获取图片的字节 pic.write(c) pic1 = Photo() pic1.pic = '{}/{}'.format(userFolder, f1.name) pic1.author = request.user pic1.save() return HttpResponse('OK')
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
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())
def test_build_faces_from_photo(self): photo = Photo(path="/0e67e4b99c2c4028a7bfc9c7dabe5152_r.jpg") photo.save() face = Face.build_faces_from_photo(photo) self.assertIsNotNone(face)