Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >无法将操作数与形状(128,) (0,)错误一起广播

无法将操作数与形状(128,) (0,)错误一起广播
EN

Stack Overflow用户
提问于 2019-04-10 21:49:07
回答 1查看 887关注 0票数 4

我正在尝试实现一个面部识别登录系统,但我遇到错误“操作数无法与形状(128,) (0,)一起广播”,我不知道该如何解决它。下面是我已经实现的view.py和facedetector.py,以及我从服务器得到的错误:

错误

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Traceback (most recent call last):
File "C:\django-projects\lib\site packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\django-projects\lib\site-packages\django\core\handlers\base.py", 
line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\django-projects\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\django-projects\aps\aps_site\authenticate\views.py", line 54, in login_user
if facedect(user.userprofile.head_shot.url):
File "C:\django-projects\aps\aps_site\authenticate\views.py", line 37, in facedect
check=face_recognition.compare_faces(face_1_face_encoding, face_encodings)
File "C:\django-projects\lib\site-packages\face_recognition\api.py", line 222, in compare_faces
return list(face_distance(known_face_encodings, face_encoding_to_check) <= tolerance)
File "C:\django-projects\lib\site-packages\face_recognition\api.py", line 72, in face_distance
return np.linalg.norm(face_encodings - face_to_compare, axis=1) 
ValueError: operands could not be broadcast together with shapes (128,) (0,)

views.py

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout, 
update_session_auth_hash
from django.contrib.auth.forms import UserCreationForm, UserChangeForm, PasswordChangeForm
from django.contrib import messages
from .forms import SignUpForm, EditProfileForm
from django.urls import path, include
import os
import face_recognition
import cv2 

# Create your views here.

def home(request):
    return render(request, 'authenticate/home.html', {})

def facedect(loc):
    cam = cv2.VideoCapture(0)   
    s, img = cam.read()
    if s:   

            BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
            MEDIA_ROOT =os.path.join(BASE_DIR,'aps_site')

            loc=(str(MEDIA_ROOT)+loc)
            face_1_image = face_recognition.load_image_file(loc)
            face_1_face_encoding = face_recognition.face_encodings(face_1_image)[0]

            #

            small_frame = cv2.resize(img, (0, 0), fx=0.25, fy=0.25)

            rgb_small_frame = small_frame[:, :, ::-1]

            face_locations = face_recognition.face_locations(rgb_small_frame)
            face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

            check=face_recognition.compare_faces(face_1_face_encoding, face_encodings)


            print(check)
            if check[0]:
                    return True

            else :
                    return False    


def login_user(request):
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(request, username=username,password=password)
    if user is not None:
        if facedect(user.userprofile.head_shot.url):
            login(request, user)
            messages.success(request,('You have successfully logged in!'))
        return redirect('home')
    else:
        messages.success(request, ('Error logging in!-Please try again'))
        return redirect('login')
else:
    return render(request, 'authenticate/login.html', {})

def logout_user(request):
    logout(request)
    messages.success(request, ('You have been logged out!'))
    return redirect('login')

def register_user(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data['username']
            password = form.cleaned_data['password1']
            user = authenticate(username = username, password = password)
            login(request, user)
            messages.success(request, ('You have registered...'))
            return redirect('home')
    else:
        form = SignUpForm()

    context = {'form' : form}
    return render(request, 'authenticate/register.html', context)

def edit_profile(request):
    if request.method == 'POST':
        form = EditProfileForm(request.POST, instance=request.user)
        if form.is_valid():
            form.save()
            messages.success(request, ('You have edited your profile...'))
            return redirect('home')
    else:
        form = EditProfileForm(instance=request.user) #

    context = {'form' : form}
    return render(request, 'authenticate/edit_profile.html', context)

def change_password(request):
    if request.method == 'POST':
        form = PasswordChangeForm(data=request.POST, user=request.user)
        if form.is_valid():
            form.save()
            update_session_auth_hash(request, form.user)
            messages.success(request, ('You have changed your password...'))
            return redirect('home')
    else:
        form = PasswordChangeForm(user=request.user) #

    context = {'form' : form}
    return render(request, 'authenticate/change_password.html', context)

facedetector.py

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import os
from django.urls import path, include
import face_recognition
import cv2 
from PIL import Image #?


#initialize the camera
def facedect(loc):
    cam = cv2.VideoCapture(0)   # 0 -> index of camera
    s, img = cam.read()
    if s:   
            # frame captured without any errors
            cv2.namedWindow("image_test")
            cv2.imshow("image_test",img)
            #cv2.waitKey(0) # waits until a key is pressed
            cv2.destroyWindow("image_test")
            cv2.imwrite("captured-image.png",img) #to save the captured image to the directory file

            BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
            MEDIA_ROOT =os.path.join(BASE_DIR,'aps_site')

            print(MEDIA_ROOT,loc)
            loc=(str(MEDIA_ROOT)+loc)
            print(loc)
            print("C:\django-projects\aps\aps_site\aps_site\media\profile_images")
            face_1_image = face_recognition.load_image_file(loc)
            face_1_face_encoding = face_recognition.face_encodings(face_1_image)[0]

            small_frame = cv2.resize(img, (0, 0), fx=0.25, fy=0.25)

            rgb_small_frame = small_frame[:, :, ::-1]

            face_locations = face_recognition.face_locations(rgb_small_frame)
            face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

            check=face_recognition.compare_faces(face_1_face_encoding, face_encodings)

            print(check)
            if check[0]:
                    return True

            else :
                    return False    

facedect('C:\media\profile_images')
EN

回答 1

Stack Overflow用户

发布于 2019-07-18 18:26:25

face_recognition.compare_faces函数的第一个参数应该是stated in the documentation形式的列表。将您的django-projects\aps\aps_site\authenticate\views.py第37行更改为:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
check=face_recognition.compare_faces([face_1_face_encoding], face_encodings)

来解决异常的原因。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55621789

复制
相关文章
[爬虫]Scrapy 错误ordinal not in range(128)
UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xb0 in position 1: ordinal not in range(128)
周小董
2019/03/25
1.5K0
[爬虫]Scrapy 错误ordinal not in range(128)
LeetCode 1658. 将 x 减到 0 的最小操作数(哈希)
给你一个整数数组 nums 和一个整数 x 。每一次操作时,你应当移除数组 nums 最左边或最右边的元素,然后从 x 中减去该元素的值。请注意,需要 修改 数组以供接下来的操作使用。
Michael阿明
2021/02/19
8810
NumPy中的广播:对不同形状的数组进行操作
NumPy是用于Python的科学计算库。它是数据科学领域中许多其他库(例如Pandas)的基础。
deephub
2021/01/12
3K0
NumPy中的广播:对不同形状的数组进行操作
问与答128:如何找到最接近0的数值?
Q:有一列数值,我想找到与0最接近的数值是什么,如下图1所示,可以看出单元格A9中的数值1最接近0,我使用什么公式才能找到该值?
fanjy
2021/06/01
1.1K0
问与答128:如何找到最接近0的数值?
-128 == -128 ?| Integer
之前一直没有注意到 Integer 类型的判断问题,我认为 “在数值判断中,== 和 equals 的效果是相同的”,直到今天写题发现包装类下的“不能”使用 == 进行判断。
做棵大树
2022/09/27
2360
程序无法启动0xc0000005咋做_应用程序错误0xc0000005
大家在使用电脑的时候有没有遇到过0xc0000005错误问题呢?很多朋友在打开应用程序的时候就弹出0xc0000005问题,找了半天都没找到正确解决方法。那就来瞧瞧小编整理了修复0xc0000005的方法吧。
全栈程序员站长
2022/11/10
2.8K0
程序无法启动0xc0000005咋做_应用程序错误0xc0000005
[阿里DIN] 从模型源码梳理TensorFlow的乘法相关概念
本文基于阿里推荐 DIN 和 DIEN 代码,梳理了下深度学习一些概念,以及TensorFlow中的相关实现。
罗西的思考
2020/11/11
1.7K0
解决打印机报错:操作无法完成(错误0x00000709)。
解决:操作无法完成(错误0x00000709)。再次检查打印机名称,并确保打印机已连接到…
全栈程序员站长
2022/07/15
4.5K0
LeetCode 2169. 得到 0 的操作数
例如,num1 = 5 且 num2 = 4 ,应该用 num1 减 num2 ,因此,得到 num1 = 1 和 num2 = 4 。 然而,如果 num1 = 4且 num2 = 5 ,一步操作后,得到 num1 = 4 和 num2 = 1 。
Michael阿明
2022/03/10
1290
Integer 128 == 128,true?
Integer数据类型是我们经常用到的一种数据类型,如果不了解它的特性,可能会造成一些意料不到的情况出现,有时甚至会引发线上事故。
小诸葛
2020/04/14
1.1K0
广播与多播
上一篇聊了UDP相关的知识点,包含UDP有什么特点、为什么需要进行IP分片、TCP与UDP有何区别等。
Liusy
2021/05/14
1.5K0
Excel应用程序无法正常启动0xc0000142; office 出现应用程序错误无法正常启动(0xc0000142);
今天晚上,在我吃完晚饭重新返回实验室;本来想看一下excel表中的数据,但是出现了 excel 无法正常启动的 0xc0000142错误。通过搜索,找到了解决方法。关键原因是 Microsoft Office 服务坏了,重启启动一下即可。
西湖醋鱼
2020/12/31
3.2K0
共享打印机无法连接打印,错误代码0x0000011b_打印机共享错误0x000001
Win10电脑1直连的打印机,设备了共享。 从另一个电脑2访问电脑1的共享打印机,连接提示错误0x0000011b,如下:
全栈程序员站长
2022/09/30
1.3K0
共享打印机无法连接打印,错误代码0x0000011b_打印机共享错误0x000001
windows无法连接到打印机错误为0x000000011b_无法连接到打印机错误0000011b
最近打印机连不上,查了下网上的资料,发现是Windows10的一个更新bug导致,但是按照网上的方法视乎重启后windows会强制更新,还是无法彻底解决问题。于是在继续查找到相关资料,现在将解决方法记录下来。
全栈程序员站长
2022/09/30
3.6K0
windows无法连接到打印机错误为0x000000011b_无法连接到打印机错误0000011b
tf.sparse
张量流将稀疏张量表示为三个独立的稠密张量:指标、值和dense_shape。在Python中,为了便于使用,这三个张量被收集到一个SparseTensor类中。如果有单独的指标、值和dense_shape张量,在传递到下面的ops之前,将它们包装在sparse张量对象中。具体来说,稀疏张量稀疏张量(指标、值、dense_shape)由以下分量组成,其中N和ndims分别是稀疏张量中的值和维数:
狼啸风云
2019/09/06
1.9K0
应用程序无法启动0xc0000005怎么解决_错误代码0x000000A5
应用程序可以满足不同用户的需求,帮助我们解决不同问题,拓展了计算机领域,给我们使用电脑提供了方便。如果打开应用程序时出现0xc0000005错误怎么办呢?接下来,我就将详细的解决方法分享给你们
全栈程序员站长
2022/11/09
2.7K0
应用程序无法启动0xc0000005怎么解决_错误代码0x000000A5
如何将Redux与React Hooks一起使用
React Redux在2019年6月11日发布的7.1版中提供了对Hooks的支持。这意味着我们可以在函数组件中将Redux与Hooks一起使用,而不是使用高阶组件(HOC)。
前端知否
2020/03/23
7K0
如何将Redux与React Hooks一起使用
错误代码0xc0000005是什么错误_错误状态0xc0000006
在使用电脑过程中经常会出现某些错误代码,如0xc0000005错误故障,造成电脑无法正常运行,这要怎么办呢?今天小编和大家分享电脑出现0xc0000005错误代码的解决方法。
全栈程序员站长
2022/11/10
1.3K0
错误代码0xc0000005是什么错误_错误状态0xc0000006
从0系统学Android--5.2 发送广播
前面已经学习了如何接受广播了,下面来学习如何发送自定义广播,广播类型分为:标准广播和有序广播,下面分别来说一下这两种广播如何发送。
开发者
2020/03/23
5450
从0系统学Android--5.2 发送广播
出现“内部错误,无法显示”
This page contains the following errors: error on line 2 at column 6: XML declaration allowed only at the start of the document Below is a rendering of the page up to the first error. 提示信息是头部有错误,我登录后台查看我修改过的页面,然后找到home.php我看了十几分钟没有发现那里有错误~~ 莫非头部不能有空格? 去掉试试
苦咖啡
2018/05/07
3.2K0

相似问题

numpy数组错误:操作数无法与形状(0,) (10,)一起广播

112

为什么Numpy抛出此错误ValueError:操作数无法与形状一起广播(3,0) (128,)

114

ValueError:操作数无法与形状(3,) (0,)一起广播

10

ValueError:操作数无法与形状(0,)一起广播(784,)

136

操作数无法与Pandas Dataframe的形状错误一起广播

118
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文