0

I'm making smartmirror with opencv. I use raspbian but, I have a problem.

Import the necessary packages

from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
def detect(img, cascade):
rects = cascade.detectMultiScale(img, scaleFactor=1.3, minNeighbors=4, 
                      minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE)
if len(rects) == 0:
    return []

rects[:,2:] += rects[:,:2]
return rects

def draw_rects(img, rects, color):
for x1, y1, x2, y2 in rects:
    cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)

Initialize the camera and grab a reference to the raw camera capture

camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))
cascade = cv2.CascadeClassifier("opencv-3.3.0/data/haarcascades/haarcascade_frontalface_alt.xml")

Allow the camera to warmup

time.sleep(0.1)
scan = 0

Capture frames from the camera

for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):

    # grab the raw NumPy array representing the image, then initialize the timestamp
    # and occupied/unoccupied text
    img = frame.array
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray = cv2.equalizeHist(gray)
    rects = detect(gray, cascade)
    if len(rects) != 0:
        scan = 1
    vis = img.copy()
    draw_rects(vis, rects, (0, 255, 0))
    # show the frame
    cv2.imshow("Frame", vis)
    key = cv2.waitKey(1) & 0xFF
    # clear the stream in preparation for the next frame
    rawCapture.truncate(0)



# if the `q` key was pressed, break from the loop

if scan == 1:
    break

so, In a virtual environment, I want to take variable(scan) into my file(.py). but, I get an error, No module named cv2 how can I use the variable(scan) into my file??? help me, please.

This is my file(.py)

#smartmirror.py
from __future__ import print_function
from aplclient.discovery import build
from httplib2 import Http
from oauth2client import file, clinet, tools
from Tkinter import *
import locale

(used coding)
kangaroo_cliff
  • 6,067
  • 3
  • 29
  • 42
  • 1
    Please, proof read your post and fix the formatting. This mess is unreadable. – Dan Mašek May 07 '18 at 20:48
  • Did you pip install cv2? Looks similar to this older problem on here. Check out this top answer and see if you are still having a problem and update [OpenCV - cannot find module cv2](https://stackoverflow.com/questions/19876079/opencv-cannot-find-module-cv2) – Keri M. May 07 '18 at 23:37

0 Answers0