Reputation: 846
I am trying to find all the group pic that contains a particular face. I have all those pic in "D:\\Camera"
dir. I have my code in "E:\\ImageFinder\main.py"
and my reference image at "E:\\ImageFinder\referencePic\image.png"
. I have created a code that runs almost forever because the "Camera" dir contains more than 500 images. It makes it take forever.
My code without Threads:
from typing import Generator, List
import face_recognition
import os
import threading
import asyncio
from time import sleep
class ImageMatcher:
def __init__(self, sourceImagePath, targerDirPath) -> None:
self.sourceImagePath = sourceImagePath
self.targetDirPath = targerDirPath
self.imageTypes = ["jpg", "png", "jpeg"]
self.matchmaking = False
self.matchedImages = []
self.known_image = face_recognition.load_image_file(self.sourceImagePath)
self.known_encoding = face_recognition.face_encodings(self.known_image)[0]
def getTargetImageFilePaths(self):
files: List[str] = os.listdir(self.targetDirPath)
for file in files:
for ext in self.imageTypes:
if f".{ext}" in file.lower().strip():
yield os.path.join(self.targetDirPath, file)
break
def imagesMatch(self, targetImage: str) -> bool:
unknown_image = face_recognition.load_image_file(targetImage)
unknown_encoding = face_recognition.face_encodings(unknown_image, model = "small")
if len(unknown_encoding) > 0:
unknown_encoding = unknown_encoding[0]
else:
return False
matched = any(face_recognition.compare_faces([self.known_encoding], unknown_encoding))
if matched:
print("found")
self.matchedImages.append(targetImage)
return matched
def searchAll(self):
imgFiles = self.getTargetImageFilePaths()
result = []
for imgFile in imgFiles:
result.append(self.imagesMatch(imgFile))
print(result)
return result
matchMyFace = ImageMatcher("registeredPics\\103\\image.png", "D:\\Camera")
matchMyFace.searchAll()
I tried putting the "imageMatch" in _thread, and even in async io. But nothing changed there. The code in threading or asyncio processes only 15-20 images... no error raised.
Something I noticed when threading is that after the 15-20 images are matched the print statement does not print when placed below the line: unknown_encoding = face_recognition.face_encodings(unknown_image, model = "small")
, and a print statement above it works fine in all the 500 images. Hope this info is helpful.
The issue is:
thread
or asyncio
not working?Upvotes: 0
Views: 17