Python Function is not executed in the output - python

I am creating a camera application using opencv and pyautogui.The function is not getting evaluated.
from utils import CFEVideoConf, image_resize
def recog():
cap = cv2.VideoCapture(0)
save_path = 'saved-media/video.avi'
frames_per_seconds = 24.0
config = CFEVideoConf(cap, filepath=save_path, res='720p')
out = cv2.VideoWriter(save_path, config.video_type, frames_per_seconds, config.dims)
while (True):
# Capture frame-by-frame
ret, frame = cap.read()
out.write(frame)
# Display the resulting frame
cv2.imshow('frame',frame)
if cv2.waitKey(20) & 0xFF == ord('q'):
op = pyautogui.confirm("")
if op == 'OK':
print("Out")
break
cap.release()
out.release()
cv2.destroyAllWindows()
opt =pyautogui.confirm(text= 'Chose an option', title='Camcorder', buttons=['Record', 'Capture', 'Exit'])
if opt == 'START':
print("Starting the app")
recog()
if opt == 'Exit':
print("Quit the app")
Please correct the mistakes if there are any.

The function recog is not getting evaluated because of below if statement
if opt == 'START':
There is no button named START. Your confirm() function displays a message box with Record, Capture, Exit buttons, but no START button. So, there is no way confirm() function returned value will be equal to START.

Related

PyQt5 MdAria Open camera in the same window

I am creating an application in which i need that in one page there is a button and one view. When i click on that button i want to open camera in that view only not separate view of camera. Is it possible? And if yes, please provide me some hint or code. Thank you.
This code opens the camera separately and not in the designated area
enter code here
def onclicked(self):
u_nam = self.txt_name
img_counter = 0
self.cap = cv2.VideoCapture(0)
while self.cap.isOpened():
ret, frame = self.cap.read()
frame = cv2.flip(frame, 1)
if ret == True:
cv2.imshow(' ', frame)
k = cv2.waitKey(1)
if k%256 == 27:
# ESC pressed
print("Escape hit, closing...")
break
elif k%256 == 32:
#elif k == btn1:
# SPACE pressed
cv2.imwrite("registered/{u_nam}/frame_{img_counter}.jpg", frame)
print("Successfully captured!")
ret, frame = self.cap.read()
img_counter += 1
self.cap.release()
cv2.destroyAllWindows()

Mouse input outside the window

I am trying to figure out how to replace keyboard press into mouse left click..
I tried pynput, but for somewhat reason it doesn't seem to be working. Can anyone help with this?
IS there a way to receive this input even while doing something else? For example browsing the internet and still getting screenshots when mouse is clicked.
cam = cv2.VideoCapture(1)
cv2.namedWindow("test")
img_counter = 0
while True:
ret, frame = cam.read()
if not ret:
print("failed to grab frame")
break
cv2.imshow("test", frame)
k = cv2.waitKey(1)
if k%256 == 27:
# ESC pressed
print("Escape hit, closing...")
break
elif k%256 == 32:
# SPACE pressed
img_name = "opencv_frame_{}.png".format(img_counter)
cv2.imwrite(img_name, frame)
print("{} written!".format(img_name))
img_counter += 1
cam.release()
cv2.destroyAllWindows()

Error using cv2 to train the camera (Python)

I am trying to train my camera using cv2 library. I am using a camera connected to Raspberry Pi3. it works fine since i tested the camera. However whenever I try to using the camera for face recognition later I keep getting this error:
return compile(source, filename, mode, PyCF_ONLY_AST)
IndentationError: expected an indented block
this is the code below:
import cv2
name = 'Mohammed' #replace with your name
cam = cv2.VideoCapture(0)
cv2.namedWindow("press space to take a photo", cv2.WINDOW_NORMAL)
cv2.resizeWindow("press space to take a photo", 500, 300)
img_counter = 0
while True:
ret, frame = cam.read()
if not ret:
print("failed to grab frame")
break
cv2.imshow("press space to take a photo", frame)
k = cv2.waitKey(1)
if k%256 == 27:
# ESC pressed
print("Escape hit, closing…")
break
elif k%256 == 32:
# SPACE pressed
img_name = "dataset/"+ name +"/image_{}.jpg".format(img_counter)
cv2.imwrite(img_name, frame)
print("{} written!".format(img_name))
img_counter += 1
cam.release()
cv2.destroyAllWindows()

How to capture a picture in opencv

python newbie here I have following code which I'm using to capture a picture using opencv. It captures the picture when I press q key on keyboard.
Working fine so far.
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
cv2.imshow('frame', rgb)
if cv2.waitKey(1) & 0xFF == ord('q'):
out = cv2.imwrite('capture.jpg', frame)
break
cap.release()
cv2.destroyAllWindows()
I need it to capture the picture when I give a command (like 'Capture now'). Can anyone help me how to capture a frame when user gives the written command rather than by pressing the key. Thanks
You can write like
reqCommand = 'Capture_pic'
command = input('Enter command')
if command == reqCommand:
out = cv2.imwrite('capture.jpg', frame)
Update:
This update is to make it enable to not block the execution of the program
import cv2
import threading
command = None
def process():
while True:
command = input('Enter command')
thread = threading.Thread(target=process)
thread.start()
cap = cv2.VideoCapture(0)
reqCommand = 'Capture_pic'
while(True):
ret, frame = cap.read()
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
cv2.imshow('frame', rgb)
if command == reqCommand:
out = cv2.imwrite('capture.jpg', frame)
thread.terminate()
break
cap.release()
cv2.destroyAllWindows()

Python OpenCV - Update camera index of live webcam view

I am viewing a webcam camera live feed. I would like to incorporate this into a Tkinter GUI and have a dropdown selection that allows one to change the camera index, and therefore the webcam being used, on the fly.
How can this be achieved?
Example code:
import cv2
def show_webcam(mirror=False):
cam = cv2.VideoCapture(0)
while True:
ret_val, img = cam.read()
if mirror:
img = cv2.flip(img, 1)
cv2.imshow('my webcam', img)
if cv2.waitKey(1) == 27:
break # esc to quit
cv2.destroyAllWindows()
def main():
show_webcam(mirror=True)
if __name__ == '__main__':
main()
To change camera at run time all you need to change is the index you pass in
cv2.VideoCapture(index).
Find out how many camera you will be using for your app and for 3 cameras, you can change it through changing index to 0 or 1 or 2.
Add one more parameter as index
show_webcam(mirror=True, index)
in function side you can use this
def show_webcam(mirror=False,index):
cam = cv2.VideoCapture(index)
while True:
ret_val, img = cam.read()
if mirror:
img = cv2.flip(img, 1)
cv2.imshow('my webcam', img)
if cv2.waitKey(1) == 27:
break # esc to quit
cv2.destroyAllWindows()

Categories

Resources