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()
Related
I would like to know how I could go about playing audio from a specific position in a video file, using ffpyplayer.
I know how to set the frame I want to start playing at in opencv but not in ffplayer.
Any ideas or suggestions would help a ton. Here is what I have came up with.
import cv2
from ffpyplayer.player import MediaPlayer
video_path=videoName
def PlayVideo(video_path):
font = cv2.FONT_HERSHEY_SIMPLEX
video = cv2.VideoCapture(video_path)
video.set(cv2.CAP_PROP_POS_FRAMES, 7200)
player = MediaPlayer(video_path)
while True:
grabbed, frame = video.read()
audio_frame, val = player.get_frame()
if not grabbed:
print("End of video")
break
if cv2.waitKey(28) & 0xFF == ord("q"):
break
cv2.imshow("Video", frame)
if val != 'eof' and audio_frame is not None:
#audio
img, t = audio_frame
video.release()
cv2.destroyAllWindows()
PlayVideo(video_path)
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()
I have written a code to enable capturing images from webcam feed using OpenCV. However there is an input delay whenever I press the key to capture my frame. There is no delay when I use it to quit, but there a significant delay when I use capture. I measured this by printing a statement inside both the cases, on pressing c the statement takes a delay before printing. The problem seems to me something like...the camera resources are being used and not freed up in time for the next key press or something like that....but not sure.
import cv2 as cv
import numpy as np
import glob
import matplotlib.pyplot as plt
cap = cv.VideoCapture(1)
img_counter = 0
while True:
ret, frame = cap.read()
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
# cv.imshow('frame',frame)
cv.imshow('gray', gray)
if not ret:
break
if cv.waitKey(1) & 0xFF == ord('q'):
print('helloq')
break
elif cv.waitKey(1) & 0xFF == ord('c'):
print('hello{}'.format(img_counter))
img_name = "opencv_frame_{}.png".format(img_counter)
cv.imwrite(img_name, gray)
img_counter += 1
I am using an external web camera and
cv2.__version__ = 3.4.2`
Solved your issue, it seems like its caused by your key check.
You should not call waitKey(1) more than once. It causes lag.
Try this solution:
cap = cv.VideoCapture(0)
img_counter = 0
while True:
ret, frame = cap.read()
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
# cv.imshow('frame',frame)
cv.imshow('gray', gray)
if not ret:
break
key = cv.waitKey(1)
if key==ord('c'):
print('img{}'.format(img_counter))
img_name = "opencv_frame_{}.png".format(img_counter)
cv.imwrite(img_name, gray)
img_counter += 1
print("Succesfully saved!")
if key==ord('q'):
print('Closing cam...')
break
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()
I am using the code below to capure frames from the camera when space key is pressed and save the frame to file. This code works OK.
import cv2
cam = cv2.VideoCapture("rtsp://10.0.0.90:554/?chID=1&streamType=main&linkType=tcp")
cv2.namedWindow("test")
img_counter = 0
while True:
ret, frame = cam.read()
cv2.imshow("test", frame)
if not ret:
break
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()
I need your help to edit this code and create new loop where every half second 2 last frames will blink so I can
cv2.imshow('image', previous_frame)
time.sleep(0.5)
cv2.imshow('image', last_frame)
time.sleep(0.5)
So the task is when the programm starts show the first still frame (opencv_frame_0.png). When the space key pressed start flashing 2 images (opencv_frame_0 and opencv_frame_1) every 0.5 sec. When space key pressed again the images that will flash will be opencv_frame_1 and opencv_frame_2.
I am noob in python and opencv and need some help.
Thank you in advance.
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()