frame.shape NameError: name ‘frame’ is not defined

社会演员多 python 369

原文标题frame.shape NameError: name ‘frame’ is not defined

我对Python一窍不通,但是我有一份工作需要处理,所以我需要实现一些功能。下面是我的代码。当我运行Python时,

回溯(最后一次调用):文件“C:\Users\user\Desktop\bodypix\bodypix.py”,第 80 行,在 frame.shapeNameError:未定义名称“frame”

此错误已检查。为什么会出现此错误?如果您知道如何解决此问题,我将非常感谢您的帮助。

import tensorflow as tf
from tf_bodypix.api import download_model, load_model, BodyPixModelPaths
import cv2
from matplotlib import pyplot as plt
import numpy as np


# # 2. Detections

# In[2]:

load_model(download_model(BodyPixModelPaths.MOBILENET_FLOAT_50_STRIDE_16))

# In[3]:

bodypix_model = load_model(download_model(BodyPixModelPaths.MOBILENET_FLOAT_50_STRIDE_16))

# In[27]:

# get vid cap device
cap = cv2.VideoCapture(0) 

# loop through frame
while cap.isOpened(): 
    ret, frame = cap.read()
    
    # BodyPix Detections
    result = bodypix_model.predict_single(frame)
    mask = result.get_mask(threshold=0.5).numpy().astype(np.uint8)
    masked_image = cv2.bitwise_and(frame, frame, mask=mask)
    
    # Show result to user on desktop
    cv2.imshow('BodyPix', masked_image)
    
    # Break loop outcome 
    if cv2.waitKey(10) & 0xFF == ord('q'):
        break

cap.release() # Releases webcam or capture device
cv2.destroyAllWindows() # Closes imshow frames

# # 3. Add Virtual Background

# In[12]:

img = cv2.imread('beach.jpg')
img = img[:480, :640, :]


# In[13]:


plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

# In[14]:

img.shape


# In[15]:

frame.shape

# In[24]:

plt.imshow(mask)


# In[23]:


plt.imshow(np.where(np.add(mask, -1) == -1, 1, np.add(mask, -1)))

# In[ ]:

# get vid cap device
cap = cv2.VideoCapture(0) 

# loop through frame
while cap.isOpened(): 
    ret, frame = cap.read()
    
    # BodyPix Detections
    result = bodypix_model.predict_single(frame)
    mask = result.get_mask(threshold=0.5).numpy().astype(np.uint8)
    masked_image = cv2.bitwise_and(frame, frame, mask=mask)
    
    # Apply virtual background
    neg = np.add(mask, -1)
    inverse = np.where(neg==-1, 1, neg).astype(np.uint8)
    masked_background = cv2.bitwise_and(img, img, mask=inverse)
    final = cv2.add(masked_image, masked_background)
    
    # Show result to user on desktop
    cv2.imshow('BodyPix', final)
    
    # Break loop outcome 
    if cv2.waitKey(10) & 0xFF == ord('q'):
        break

cap.release() # Releases webcam or capture device
cv2.destroyAllWindows() # Closes imshow frames

原文链接:https://stackoverflow.com//questions/71918686/frame-shape-nameerror-name-frame-is-not-defined

回复

我来回复
  • ericksonb的头像
    ericksonb 评论

    您在frame被分配之前引用frame.shape。注意img.shape是如何被允许的,因为在此之前有一个赋值:img = cv2.imread('beach.jpg')

    阅读文档以查看应分配给哪个“框架”。

    2年前 0条评论