trying to compare a cv2 window to a saved jpeg - python

so as the title says, im trying to compare a cv2 generated live window to a jpeg i have already saved to help create a decently advanced macro im working on.
my solution to this was to repeatedly save the live stream to a file on my pc so that i instead only have to compare image to image but its proving to be more difficult than i thought, idk what exactly is wrong either (i was following only the first part of this https://www.youtube.com/watch?v=ks4MPfMq8aQ&ab_channel=sentdex tutorial for the creation of the live window)
import numpy as np
import PIL
import keyboard
from PIL import ImageGrab
import cv2
import time
last_time = time.time()
path = r'C:\Users\srajp\OneDrive\Desktop\Pranav (temporary)\gaming.jpg'
img = cv2.imread(path)
path2 = r'C:\Users\srajp\OneDrive\Desktop\Pranav (temporary)\maging.jpg'
img2 = cv2.imread(path2)
window_name = 'hackerman'
time.sleep(3)
while True:
screen = np.array(ImageGrab.grab(bbox=(0,0,1920,1008)))
last_time = time.time()
cv2.imshow(window_name, cv2.cvtColor(screen, cv2.COLOR_BGR2RGB))
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
result=cv2.imwrite(path2, screen)
if result==False:
print("Error in saving file")
img22 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
difference = cv2.subtract(img22, img)
r, g, b = cv2.split(difference)
if cv2.countNonZero(r) == 0 and cv2.countNonZero(g) == 0 and cv2.countNonZero(b) == 0:
print('wNIuafiuwfaygewfia')
thank you for answering in advance, and i apoligise for any confusing or errored formating as this is my first post, i will read the replies if there are any tomorrow as it is late at night in my timezone as of writing this
https://i.stack.imgur.com/nCT0o.jpg
this is the image that saves live
https://i.stack.imgur.com/XyNix.jpg
thats the default image,
the default image is just my desktop for now but the theory is that i can add multiple if statements for different scenarios that occur while using the macro

Related

Problem with naming a camera captured file in a while-loop

This is my code, It takes a picture via webcam and saves it in a folder I specify. The name is then "day-time_0/1/2/3.jpg".
The problem is I would like to have it without "_0/1/2/3". But these numbers are necessary for the loop I think at least. But if I take out this count it saves a picture and over saves it every time I want to make another picture in the same interface.
Is there a way that if I am in the same interface and take several pictures that I can save them again and again with the new current time?
import cv2
import os
import time
timestr = time.strftime("%Y%m%d-%H%M%S")
def save_frame_camera_key(device_num, dir_path, basename, ext='jpg', delay=1, window_name='frame'):
cap = cv2.VideoCapture(device_num)
if not cap.isOpened():
return
os.makedirs(dir_path, exist_ok=True)
base_path = os.path.join(dir_path, basename)
n = 0
while True:
ret, frame = cap.read()
cv2.imshow(window_name, frame)
key = cv2.waitKey(delay) & 0xFF
if key == ord('c'):
cv2.imwrite('{}_{}.{}'.format(base_path, **n**, ext), frame)
** n += 1**
elif key == ord('q'):
break
cv2.destroyWindow(window_name)
save_frame_camera_key(0, 'data/temp', timestr)
As i said i tried deleting the count. Didnt work. Ive tried it with an different capture Code and it didnt work.

Displaying a Text from a Video but avoiding the same text

i am trying to do a program where it detects the video and capture every 100th frame then display the output. the problem that i faced here is that there will be duplication for example, the current pattern for the output is (1,1,1,2,2,3,3,2,1) but the result that i want to show is (1,2,3,2,1). To sum up, not displaying the same text continuously but allowed to be displayed when the previous image is different with the current ones. I have tried coming up with an idea of removing duplicate images but it will not give the expected result as it will delete all the images.
from PIL import Image
import pytesseract
from wand.image import Image as Img
import nltk
from nltk.tokenize import word_tokenize
from nltk.tag import pos_tag
import numpy as np
import os
import cv2
image_frames = 'image_frames'
def files():
#auto create image frames file
try:
os.remove(image_frames)
except OSError:
pass
if not os.path.exists(image_frames):
os.makedirs(image_frames)
# specify the source video path
src_vid = cv2.VideoCapture('15sec.mp4')
return(src_vid)
def process(src_vid):
# Use an index to integer-name the files
index = 0
while (src_vid.isOpened()):
ret, frame = src_vid.read()
if not ret:
break
# name each frame and save as png / jpeg
name = './image_frames/frame' + str(index) + '.jpeg'
if index % 100 == 0:
print('Extracting frames...' + name)
cv2.imwrite(name, frame)
index = index + 1
if cv2.waitKey(10) & 0xFF == ord('q'):
break
src_vid.release()
cv2.destroyAllWindows()
# do image to text on each png
def get_text():
for i in os.listdir(image_frames):
print(str(i))
my_image = Image.open(image_frames + "/" + i)
pytesseract.pytesseract.tesseract_cmd = r'C:\Users\USERS\AppData\Local\Tesseract-OCR\tesseract.exe'
text = pytesseract.image_to_string(my_image, lang='eng')
print(text)
# main driver
if __name__ == '__main__':
vid = files()
process(vid)
get_text()

Face_recognition list out of range error in python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Hy guys based on then last question that I asked in which I haven't gotten a correct answer.
I want to write a program with face_recognition library. Its purpose is to save an image clip of a new face to a folder, updates the folder so as not to save the same face twice. I want the program to save an image to folder whenever it encounters a new face in the input video. But for now it doesn't seem to work. Sometimes it saves the whole clip of the same face to the folder which is not what I want. Can anyone help me with this code. I modified this code from a tutorial from #Murtaza I saw on youtube.
I still get this error
Traceback (most recent call last):
File "C:/Users/CHIJINDU/AppData/Roaming/JetBrains/PyCharmEdu2020.1/scratches/KRecUnknownFace2.py", line 26, in <module>
encodelistKnown = find_encodings(images)
File "C:/Users/CHIJINDU/AppData/Roaming/JetBrains/PyCharmEdu2020.1/scratches/KRecUnknownFace2.py", line 21, in find_encodings
encode = face_recognition.face_encodings(img)[0]
IndexError: list index out of range
This is the improved code below.
import face_recognition
import cv2
import os
import numpy as np
path = r"C:\Users\CHIJINDU\Desktop\KArtIntel"
images = []
class_names= []
myList = os.listdir(path)
for cl in myList:
curImg= cv2.imread(f'{path}\{cl}')
images.append(curImg)
class_names.append(os.path.splitext(cl)[0])
print(class_names)
def find_encodings(images):
encodelist = []
for img in images:
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
encode = face_recognition.face_encodings(img)[0]
#encode= face_recognition.face_encodings(img)[0]
encodelist.append(encode)
return encodelist
encodelistKnown = find_encodings(images)
print(len(encodelistKnown))
cap = cv2.VideoCapture(r"C:\Users\CHIJINDU\Desktop\Elastic.mp4")
while True:
success, img = cap.read()
imgS = cv2.resize(img, (0,0), None, 0.25,0.25)
imgS =cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
faceCurFrame= face_recognition.face_locations(imgS)
encodesCurFrame= face_recognition.face_encodings(imgS, faceCurFrame)
for encodeFace, faceloc in zip(encodesCurFrame, faceCurFrame):
matches = face_recognition.compare_faces(encodelistKnown,encodeFace)
faceDis = face_recognition.face_distance(encodelistKnown,encodeFace)
matchIndex= np.argmin(faceDis)
i=0
if encodesCurFrame not in encodelistKnown:
newImg= cv2.imwrite(r'C:\Users\CHIJINDU\Desktop\KArtIntel\KUDOS-J14{index}.jpg'.format(index=i), img)
images.append((newImg))
#fps = int(video_capture.get(cv2.CAP_PROP_FPS))
#print(fps)
i+=1
# Display the resulting image
cv2.imshow('Video', img)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release handle to the webcam
cap.release()
cv2.destroyAllWindows()
Random Davis is right on....i changed the portion of code to include failover and ti worked. try also with the attached file to see the different behavior with a very easy to find face
import face_recognition
import cv2
import os
import numpy as np
path = r"C:\\Users\\shawn.ramirez\\Pictures\\Scans"
images = []
class_names= []
myList = os.listdir(path)
for cl in myList:
curImg= cv2.imread(f'{path}\{cl}')
images.append(curImg)
class_names.append(os.path.splitext(cl)[0])
print(class_names)
def find_encodings(images):
encodelist = []
for img in images:
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
try:
encode = face_recognition.face_encodings(img)[0]
#encode= face_recognition.face_encodings(img)[0]
encodelist.append(encode)
return encodelist
except:
error = []
print("found no faces")
return error
encodelistKnown = find_encodings(images)
print(len(encodelistKnown))
cap = cv2.VideoCapture(r"C:\\Users\\shawn.ramirez\\Pictures\\Camera Roll\\sample.mp4")
while True:
success, img = cap.read()
imgS = cv2.resize(img, (0,0), None, 0.25,0.25)
imgS =cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
faceCurFrame= face_recognition.face_locations(imgS)
encodesCurFrame= face_recognition.face_encodings(imgS, faceCurFrame)
for encodeFace, faceloc in zip(encodesCurFrame, faceCurFrame):
matches = face_recognition.compare_faces(encodelistKnown,encodeFace)
faceDis = face_recognition.face_distance(encodelistKnown,encodeFace)
matchIndex= np.argmin(faceDis)
i=0
if encodesCurFrame not in encodelistKnown:
newImg= cv2.imwrite(r'C:\Users\CHIJINDU\Desktop\KArtIntel\KUDOS-J14{index}.jpg'.format(index=i), img)
images.append((newImg))
#fps = int(video_capture.get(cv2.CAP_PROP_FPS))
#print(fps)
i+=1
# Display the resulting image
cv2.imshow('Video', img)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release handle to the webcam
cap.release()
cv2.destroyAllWindows()

cv2.imread error:img = cv2.imread(0) SystemError: <built-in function imread> returned NULL without setting an error

I was making a simple program to take an image from the camera but a strange error has occurred:
Traceback (most recent call last):
File "the path was deleted for stackoverflow", line 3, in <module>
cv2.imshow("",img)
TypeError: Expected Ptr<cv::UMat> for argument 'mat'
And here is the simple code:
import cv2
img = cv2.imread(0)
cv2.imshow("",img)
pls help
as Roland mentioned in comment
you should provide path to image you want to open
import cv2
imageArray = cv2.imread('path/to/image.jpg')
cv2.imshow("",imageArray)
if you want to read from a camera, imread is the wrong procedure. its purpose is to read picture files.
use OpenCV's VideoCapture instead.
The following code will take the picture when you press c from your keyboard then it will store images in your current directory. Hope this will work for you, peace!
import cv2
cap = cv2.VideoCapture(0)
width = 400
height = 300
num = 0
while True:
ret, frame = cap.read()
frame = cv2.resize (frame, (width, height))
cv2.imshow("frame", frame)
#When you press c then it would capture a picture and write on the same folder
if cv2.waitKey(1) & 0xff == ord('c'):
cv2.imwrite("./Image_"+str(num)+".jpg", frame)
num+=1
if cv2.waitKey(1) & 0xff == ord('q'):
break
cv2.destroyAllWindows()

Is there any method to extract still frames from a live video

I am working on a sign language project I just wanted to know how can I extract still frames from a live video. Is there any built-in method or i have to code any logic for it.
OpenCV allows you receive a call back for each frame from the video source and you can analyse, modify and/or save that frame as you want.
A simple example using Python might be (based on tutorial here: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html)
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
# Here we just save the frame to a file
cv2.imwrite('frame_'+str(i)+'.jpg',frame)
i+=1
# Display the resulting frame
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
For a more complex example, this time in Android, the following code will save the frame to an image file if it detects a particular color (based on the OpenCV Color Blob detection example:
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba();
if (mIsColorSelected) {
mDetector.process(mRgba);
List<MatOfPoint> contours = mDetector.getContours();
Log.e(TAG, "Contours count: " + contours.size());
Imgproc.drawContours(mRgba, contours, -1, CONTOUR_COLOR);
Mat colorLabel = mRgba.submat(4, 68, 4, 68);
colorLabel.setTo(mBlobColorRgba);
Mat spectrumLabel = mRgba.submat(4, 4 + mSpectrum.rows(), 70, 70 + mSpectrum.cols());
mSpectrum.copyTo(spectrumLabel);
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String testFilename = "TestOCVwrite.png";
File testFile = new File(path, testFilename);
String testFullfileName = testFile.toString();
Log.i(TAG, "Writing to filename: " + testFilename);
Boolean writeResult = Imgcodecs.imwrite(testFullfileName, mRgba);
if (!writeResult) {
Log.i(TAG, "Failed to write to filename: " + testFilename);
}
}
return mRgba;
}

Categories