在python中重塑anaconda上的复杂数组错误

在python中重塑anaconda上的复杂数组错误,python,opencv,computer-vision,Python,Opencv,Computer Vision,所以我遇到的问题是这个代码: import cv2 import numpy as np from sklearn.metrics import pairwise #distance calculations for later background= None accumulated_weight= 0.5 roi_top=30 roi_bottom= 300 roi_left= 600 roi_right= 300 #function to find average backgroun

所以我遇到的问题是这个代码:

import cv2
import numpy as np
from sklearn.metrics import pairwise #distance calculations for later


background= None
accumulated_weight= 0.5
roi_top=30
roi_bottom= 300
roi_left= 600
roi_right= 300

#function to find average background value
def calc_accum_avg(frame, accumulated_weight):
    global background
    if background is None:
        background= frame.copy().astype('float')

    cv2.accumulateWeighted(frame, background, accumulated_weight)    


#Segmenting the contour
def segment(frame, threshold_min=25):
    diff= cv2.absdiff(background.astype('uint8'), frame)
    ret, thresholded= cv2.threshold(diff, threshold_min, 255, cv2.THRESH_BINARY)
    contours, hierarchy= cv2.findContours(thresholded.copy(),cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    if len(contours) ==0:
        return None

    else:
        #Assuming that the biggest contour in  ROI(Region of Interest) is Hand itself
        hand_segment= max(contours, key= cv2.contourArea)
        return(thresholded, hand_segment)


def count_fingers(thresholded, hand_segment):
    conv_hull= cv2.convexHull(hand_segment)
    top= tuple(conv_hull[conv_hull[:,:,1].argmin()][0])
    bottom= tuple(conv_hull[conv_hull[:,:,1].argmax()][0])
    left= tuple(conv_hull[conv_hull[:,:,0].argmin()][0])
    right= tuple(conv_hull[conv_hull[:,:,0].argmax()][0])      

    cX= (left[0]+ right[0])//2
    cY=(top[1]+ bottom[1])//2
    distance= pairwise.euclidean_distances([cX, cY], Y= [left, right, top, bottom])[0]
    max_distance= distance.max()
    radius= int(0.9*max_distance)
    circumference= (2*np.pi*radius)
    circular_roi= np.zeros_like(thresholded[:2], dtype= 'uint8')
    cv2.circle(circular_roi, (cX, cY), radius, 255, 10)
    circular_roi= cv2.bitwise_and(thresholded, thresholded, mask= circular_roi)
    contours, hierarchy= cv2.findContours(circular_roi.copy(),cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    count=0

    for cnt in count:
        (x,y, w,h)= cv2.boundingRect(cnt)
        out_of_wrist= (cY+ (cY*0.25))>(y+h)
        limit_points= ((circumference*0.25)> cnt.shape[0])

        if out_of_wrist and limit_points:
            count +=1
    return count       


cam= cv2.VideoCapture(0)
num_frames=0

while True:
        ret, frame= cam.read()
        frame_copy= frame.copy()
        roi= frame[roi_top: roi_bottom, roi_right:roi_left]
        gray= cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
        gray= cv2.GaussianBlur(gray,(7,7),0)
        if num_frames<60 :
            calc_accum_avg(gray, accumulated_weight)
            if num_frames<=59:
                cv2.putText(frame_copy, 'WAIT, GETTING BACKGROUND',(200,300), cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255),2)
                cv2.imshow('Finger Count', frame_copy)

        else:
            hand= segment(gray)
            if hand is not None:
                thresholded, hand_segment= hand
                #Draw contours
                cv2.drawContours(frame_copy, [hand_segment+(roi_right, roi_top)],-1, (255,0,0), 5)
                fingers= count_fingers(thresholded, hand_segment)
                cv2.putText(frame_copy, str(fingers), (70,50), cv2.FONT_HERSHEY_COMPLEX, 1,(0,0,255),2)
                cv2.imshow('Thresholded', thresholded)
        cv2.rectangle(frame_copy, (roi_left, roi_top), (roi_right, roi_bottom), (0,0,255),5)
        num_frames+=1
        cv2.imshow('Finger count', frame_copy)
        k= cv2.waitKey(1) & 0xFF

        if k==27:
            break

cam.release()
cv2.destroyAllWindows()
导入cv2
将numpy作为np导入
从sklearn.metrics导入成对#距离计算以供以后使用
背景=无
累计重量=0.5
roi_top=30
roi_底部=300
roi_左=600
roi_right=300
#函数查找平均背景值
def calc_accum_平均值(帧、累计重量):
全球背景
如果背景为无:
background=frame.copy().astype('float')
cv2.累计重量(帧、背景、累计重量)
#轮廓分割
def段(帧,阈值_min=25):
diff=cv2.absdiff(background.astype('uint8'),frame)
ret,thresholded=cv2.threshold(差异,threshold\u min,255,cv2.threshold\u二进制)
轮廓,层次=cv2.findContours(threshold.copy(),cv2.RETR\u EXTERNAL,cv2.CHAIN\u APPROX\u SIMPLE)
如果len(轮廓)==0:
一无所获
其他:
#假设ROI(感兴趣区域)中的最大轮廓是手本身
手部切面=最大值(轮廓,关键点=cv2.轮廓面积)
返回(阈值化,手动段)
def计数手指(阈值、手部片段):
conv_船体=cv2.Convxhull(手动段)
top=tuple(conv_-hull[conv_-hull[:,:,1].argmin()][0])
底部=元组(conv_-hull[conv_-hull[:,:,1].argmax()][0])
左=元组(conv_-hull[conv_-hull[:,:,0].argmin()][0])
右=元组(conv_hull[conv_hull[:,:,0].argmax()][0])
cX=(左[0]+右[0])//2
cY=(顶部[1]+底部[1])//2
距离=成对。欧几里德距离([cX,cY],Y=[left,right,top,bottom])[0]
最大距离=距离。最大()
半径=整数(0.9*最大距离)
周长=(2*np.pi*半径)
圆形roi=np.zeros_like(阈值[:2],数据类型='uint8')
cv2.圆(圆形(cX,cY),半径255,10)
循环_roi=cv2。按位_和(阈值化、阈值化、掩码=循环_roi)
轮廓,层次=cv2.findContours(圆形\u roi.copy(),cv2.RETR\u外部,cv2.CHAIN\u近似\u简单)
计数=0
对于计数中的cnt:
(x,y,w,h)=cv2.boundingRect(cnt)
腕外=(cY+(cY*0.25))>(y+h)
极限点=((周长*0.25)>中心形状[0])
如果超出手腕和限制点:
计数+=1
返回计数
cam=cv2.视频捕获(0)
num_frames=0
尽管如此:
ret,frame=cam.read()
frame\u copy=frame.copy()
roi=帧[roi\u顶部:roi\u底部,roi\u右侧:roi\u左侧]
灰色=cv2.CVT颜色(roi,cv2.COLOR\u bgr2灰色)
灰色=cv2.高斯模糊(灰色,(7,7),0)
if num_帧