导入类时,obiect无法正常工作

时间:2015-06-12 22:05:41

标签: python opencv

我的文件夹中有4个模块(一个是 init .py)xml文件和单独的图片专辑。

我有 main.py

from zdjecia import pics

pic = pics()
pic.run()
pic.end()

zdjecia.py

import cv2
import time
import threading
import os
from facedetect import detektuj


class pics (threading.Thread):
    def __init__(self):
        self.camera_port = 0
        self.ramp_frames = 30
        self.camera = cv2.VideoCapture(self.camera_port)
    def get_image(self):
        retval, im = self.camera.read()
        return im
    def run(self):
        for i in xrange(self.ramp_frames):
         temp = self.get_image()
        mon = 1
        while mon <100:
             camera_capture = self.get_image()
             file = os.path.dirname(__file__)+ "/nazdjecia/test_image"+str(mon)+".png"
             if(cv2.imwrite(file, camera_capture)):
                 detektuj(file)
                 time.sleep(0.2)
                 mon+=1
             else:
                 print "camera problem"
                 self.camera.release()
                 break

    def end(self):
        self.camera.release()

pic = pics()
pic.run()
pic.end()

最后是facedetect.py

import numpy as np
import cv2

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

def detektuj(path):
    img = cv2.imread(path)
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]
        eyes = eye_cascade.detectMultiScale(roi_gray)
        for(ex,ey,ew,eh) in eyes:
            cv2.Rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
    print faces

如果我运行zdjecia.py一切都很好,但是如果我尝试从main.py开始它会进入zdjecia,运行而不是中断并说“相机问题”。

为什么只有从main开始才会出现问题?

0 个答案:

没有答案