Ejemplo n.º 1
0
def img_post(request):
    global ans
    global happiness
    global anger
    global fear
    global neutral
    global contempt
    global disgust
    global sadness
    global surprise
    global tmdbId
    global recommendation
    if request.method == 'POST':
        print('get post')
        received_json_data = json.loads(request.body)
        #print(str(received_json_data))
        imgdata = base64.b64decode(str(received_json_data['img']))
        image = Image.open(BytesIO(imgdata))
        image.show()
        tmp = "temp.jpg"
        image.save(tmp, 'JPEG')

        faces = CF.face.detect(tmp, True, False, attributes)

        if len(faces):
            print(faces)
            next_step = True
            #return JsonResponse({'success' : 'yes', 'name' : 'somebody'})
        else:
            print('cannot detect face')
            next_step = False
            return JsonResponse({'success': 'no', 'name': ''})

        if next_step == True:

            ans, happiness, anger, neutral, fear, contempt, disgust, sadness, surprise, tmdbId, recommendation = face_identity.identify(
                tmp, faces)
            print('ans  ', ans)

            #如果是陌生人
            if ans == 'stranger':
                return JsonResponse({'success': 'no', 'name': 'stranger'})
            #如果不是
            else:
                return JsonResponse({
                    'success': 'yes',
                    'name': str(ans),
                    'happiness': str(happiness),
                    'anger': str(anger),
                    'neutral': str(neutral),
                    'fear': str(fear),
                    'contempt': str(contempt),
                    'disgust': str(disgust),
                    'sadness': str(sadness),
                    'surprise': str(surprise),
                    'movie': str(tmdbId),
                    'recommandation': recommendation
                })
    return JsonResponse({'success': 'no', 'name': ''})
Ejemplo n.º 2
0
def face_post(request):
    next_step = False
    if request.method == 'POST':
        image = request.FILES.get('img')
        print('image', image)
        im = Image.open(image)
        im.show()
        tmp = "temp.jpg"
        im.save(tmp, 'JPEG')

        faces = CF.face.detect(tmp, True, False)

        if len(faces):
            print(faces)
            next_step = True
            #return JsonResponse({'success' : 'yes', 'name' : 'somebody'})
        else:
            print('cannot detect face')
            next_step = False
            return JsonResponse({'success': 'no', 'name': ''})

        if next_step == True:

            ans, happiness, anger, neutral, fear, contempt, disgust, sadness, surprise, imdbId = face_identity.identify(
                tmp, faces)

            print('ans  ', ans)
            qqq = [{
                'success': 'yes',
                'name': str(ans),
                'happiness': str(happiness),
                'anger': str(anger),
                'neutral': str(neutral),
                'fear': str(fear),
                'contempt': str(contempt),
                'disgust': str(disgust),
                'sadness': str(sadness),
                'surprise': str(surprise),
                'movie': str(imdbId)
            }]

            #如果是陌生人
            if ans == 'stranger':
                return JsonResponse({'success': 'no', 'name': 'stranger'})
            #如果不是
            else:
                # return HttpResponse(json.dumps(qqq), content_type='application/json')
                return JsonResponse({
                    'success': 'yes',
                    'name': str(ans),
                    'happiness': str(happiness),
                    'anger': str(anger),
                    'neutral': str(neutral),
                    'fear': str(fear),
                    'contempt': str(contempt),
                    'disgust': str(disgust),
                    'sadness': str(sadness),
                    'surprise': str(surprise),
                    'movie': str(imdbId)
                })
    print('get not post')
    return JsonResponse({'success': 'no', 'name': ''})