Ask Your Question
0

Unable to see and take the picture in second time

asked 2019-12-02 04:12:39 -0600

Tintuk Tomin gravatar image

updated 2019-12-02 04:54:40 -0600

supra56 gravatar image

Code:

import cv2

def method_webcam_trigger()

    key = cv2. waitKey(1)
    webcam = cv2.VideoCapture(0)
    while True:
        try:
            check, frame = webcam.read()
            cv2.imshow("Capturing", frame)
            key = cv2.waitKey(1)
            if key == ord('s'): 
                cv2.imwrite(filename='saved_img.jpg', img=frame)
                webcam.release()
                cv2.waitKey(1650)
                cv2.destroyAllWindows()
                print("Image saved!")
                webcam.release()
                cv2.destroyAllWindows()
                break
            elif key == ord('q'):
                webcam.release()
                cv2.destroyAllWindows()
                break

Abouve the code which am using. I wrote the code to take the picture in a function and the function will activate in a button click. If i click on a button then the button will call this particular function(method_webcam_trigger) and save the picture. The problem is that if i click on that button again then it wont work. if i click the button on second time then the execution will stop on below line.

cv2.imshow("Capturing", frame)

Can anybody help to solve this? Below is another link which i saw asking the same question, but did't find the answer. https://stackoverflow.com/questions/2... Please help. Thankyou

edit retag flag offensive close merge delete

Comments

1

you cannot open twice VideoCapture. Is Videocapture already opened?

LBerger gravatar imageLBerger ( 2019-12-02 04:22:41 -0600 )edit

don't release the webcam ;)

berak gravatar imageberak ( 2019-12-02 04:22:46 -0600 )edit

@LBerger saying that you cannot open twice VideoCapture. Is Videocapture already opened. Look at what're you doing in function of method_webcam_trigger. Move webcam = cv2.VideoCapture(0) out of method_webcam_trigger, And put it on top of method_webcam_trigger.

supra56 gravatar imagesupra56 ( 2019-12-02 04:41:31 -0600 )edit

By clicking button will not work. Youre doing wrong. I need to see your button click event code.

supra56 gravatar imagesupra56 ( 2019-12-02 04:47:03 -0600 )edit

@berak. Listen to @LBerger. It will not work.

supra56 gravatar imagesupra56 ( 2019-12-02 04:56:06 -0600 )edit
1

@Supra, apologies, you're right

berak gravatar imageberak ( 2019-12-02 05:03:41 -0600 )edit

@berak. No problem. I already done this before.

supra56 gravatar imagesupra56 ( 2019-12-02 05:06:15 -0600 )edit

@LBerger@berak @ Supra Thanks for your comments. But the problem remains there. I tried to take out webcam = cv2.VideoCapture(0) outside the function. But still the same.

Tintuk Tomin gravatar imageTintuk Tomin ( 2019-12-02 06:45:55 -0600 )edit

If i call the function again the excution will stops on this line cv2.imshow("Capturing", frame)

Tintuk Tomin gravatar imageTintuk Tomin ( 2019-12-02 06:49:53 -0600 )edit

3 answers

Sort by ยป oldest newest most voted
0

answered 2020-05-06 09:31:22 -0600

video_capture = cv2.VideoCapture(0)
count = 0

while True:

  print('oi')
  ret, frame = video_capture.read()
  small_frame = cv2.resize(frame, (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)

  face_labels = []
  for face_location, face_encoding in zip(face_locations, face_encodings):
    metadata = lookup_known_face(face_encoding, know_face_encodings, know_face_metadata)
    if metadata is not None:
      video_capture.release()
      cv2.destroyAllWindows()
      cv2.waitKey(10)
      return Response(metadata)

    else:
      return Response(False)
  cv2.imshow('Video', frame)
  key = cv2.waitKey(1)


  if count == 5:

    video_capture.release()
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    cv2.waitKey(1)
    return Response(False)

Had the same issue and cant solve it out, this is a signup script which works propoerly at the first time that i call it from my interface, but at a second time it just doesn't work at all. Any ideas @supra56

edit flag offensive delete link more
0

answered 2019-12-02 09:17:47 -0600

supra56 gravatar image

I noticed that you left out semi-colon in line 3. Also your're missing Trap/except. When you click button s, you didn't called method method_webcam_trigger(). As @berak said, by removing release was wrong. I also added waitKey for 10 seconds. But remember, by clicking button s, you saved filename saved_img.jpg. When you clicking buitton again, it asking you to save as. So click filename, you will add index...saved_img1.jpg, and so on.

#!/usr/bin/env python37
#Raspberry pi 3/4, OpenCV 4.1.2
#Date: December 2nd, 2019

import cv2

def method_webcam_trigger():
    key = cv2. waitKey(10)
    webcam = cv2.VideoCapture(0)
    while True:
        try:
            check, frame = webcam.read()
            cv2.imshow("Capturing", frame)
            key = cv2.waitKey(1)
            if key == ord('s'): 
                cv2.imwrite(filename='saved_img.jpg', img=frame)
                print("Image saved!")
                webcam.release()
                cv2.destroyAllWindows()
                cv2.waitKey(10)
                method_webcam_trigger()
                webcam.release()
                cv2.destroyAllWindows()
                cv2.waitKey(10)               
                break                
            elif key == ord('q'):                 
                webcam.release()
                cv2.destroyAllWindows()
                break 
        except:
              pass

method_webcam_trigger()
edit flag offensive delete link more

Comments

@Tintuk Tomin . Your code is tricking. That was very nice. Before I posted answer in here I redo it and it saved 3000 filenames in cv2.waitKey(1650) seconds. But thanks.

supra56 gravatar imagesupra56 ( 2019-12-02 09:31:33 -0600 )edit

You can also change one second if you like cv2.waitKey(1).

supra56 gravatar imagesupra56 ( 2019-12-02 09:43:33 -0600 )edit
0

answered 2019-12-02 04:24:43 -0600

mvuori gravatar image

You destroy your camera when you save the image so obviously getting the bext frame fails.

edit flag offensive delete link more

Comments

If i dont destroy the camera the window will be there. It wont be closed and the webcam will work all the time. I need to start the webcam and take the the picture using the function call.

Tintuk Tomin gravatar imageTintuk Tomin ( 2019-12-02 06:48:15 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-12-02 04:12:39 -0600

Seen: 1,287 times

Last updated: Dec 02 '19