CSR
CSR

Reputation: 141

How do I find an image similar to the input image from a directory?

I have a python program which will detect images from webcam. Now, I want to compare the image recognized by the webcam with the images in my directory and check if the exact similar image is already existing or not.

I have tried using this recognition algorithm but it does not work. The program always outputs single image no matter how different the input image is.

The input image(the image scanned by the webcam) is little blurry like this while the image in the data set looks like this

I need an algorithm which can recognize these images with more accuracy.

Upvotes: 2

Views: 2382

Answers (2)

CuCaRot
CuCaRot

Reputation: 1308

Here i write a small script for you, hope that it could solve your problem

import cv2
import os
import numpy as np

from typing import Union

def read_img_from_dir(image_dir: str, query_shape: Union[list, tuple]) -> tuple[list, np.ndarray]:
    name_image = []
    img_array = np.empty((0, np.prod(query_shape)), dtype=np.float32)
    for image_name in os.listdir(image_dir):
        name_image.append(image_name)
        image = cv2.imread(os.path.join(image_dir, image_name))
        if not isinstance(image, np.ndarray):
            # if path is not image
            continue
        image = cv2.resize(image, query_shape[:2][::-1])
        image = image.reshape(1, -1) / 255.
        img_array = np.concatenate((img_array, image))
    return name_image, img_array    

def find_by_knn(query_img: np.ndarray, list_name: list[str], database: np.ndarray) -> str:
    query_img = query_img.reshape(1, -1) / 255.
    dists = np.sqrt(np.sum((database-query_img) ** 2,axis = 1))
    idx = dists.argmin()
    return list_name[idx]

if __name__=='__main__':
    image_query = 'Path to the image that you want to find'
    image_dir = 'Image folder that may contain your query image'
    img = cv2.imread(image_query)
    
    # optional: since the query image size maybe large, resize 
    # all image to a small disired shape may avoid OOM problem
    # and boost computing speed
    
    # global_shape = (320, 320)
    # img = cv2.resize(img, global_shape)
    
    shape = img.shape
    name_image, img_array = read_img_from_dir(image_dir, shape)
    result = find_by_knn(img, name_image, img_array)
    print(result)

If you wanna know more about KNN, take a look at this link: http://cs231n.github.io/classification/#nn

Upvotes: 2

Ayush Singh
Ayush Singh

Reputation: 53

IMAGE SEARCH by using python openCV.

LINK FOR open CV

opencv_python‑4.1.0+contrib‑cp35‑cp35m‑win_amd64.whl

download the import cv2

import numpy as np

from matplotlib import pyplot as plt

img = cv2.imread('watch.jpg',cv2.IMREAD_GRAYSCALE)

cv2.imshow('image',img)
cv2.waitKey(0)

cv2.destroyAllWindows()

pip install numpy

pip install matplotlib

Matplotlib is used for displaying frames from video or images.

Numpy is used for all things "numbers and Python." We are mainly making use of Numpy's array functionality.

   import cv2

import numpy as np

from matplotlib import pyplot as plt

img = cv2.imread('watch.jpg',cv2.IMREAD_GRAYSCALE)

cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Firstly we are going to import few things Next, we define img to be cv2.read(image file, parms).

The default is going to be IMREAD_COLOR, which is color without any alpha channel.

For the second parameter, you can use -1, 0, or 1. Color is 1, gray scale is 0, and the unchanged is -1. Thus, for gray scale, one could do img = cv2.imread('watch.jpg', 0)

Once loaded, we use cv2.imshow(title,image) to show the image. From here, we use the cv2.waitKey(0) to wait until any key is pressed. Once that's done, we use cv2.destroyAllWindows() to close everything.

Loading Video Source Open CV Python with video and webcams.

handling frames from a video is identical to handling for images.

code--

 import numpy as np

import cv2

cap = cv2.VideoCapture(0)

 while(True):

ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
 cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):

    break

cap.release()

cv2.destroyAllWindows()

we import numpy and cv2 Next, we cay cap = cv2.VideoCapture(0).

  while(True):
ret, frame = cap.read()

we have ret and frame being defined as the cap.read().

  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

*we define a new variable, gray, as the frame, converted to gray.

note**

*OpenCV reads colors as BGR (Blue Green Red), where most computer applications read as RGB (Red Green Blue).

 cv2.imshow('frame',gray)

Here, we're showing the converted-to-gray feed.

if cv2.waitKey(1) & 0xFF == ord('q'):
  break

if key is a q, we will exit the while loop with a break, which then runs:

cap.release()
cv2.destroyAllWindows()

This releases the webcam, then closes all of the imshow() windows.

if you want to save the recording and then process it by using,

 import numpy as np
import cv2

cap = cv2.VideoCapture(1)


fourcc = cv2.VideoWriter_fourcc(*'XVID')

out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))


while(True):

ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
out.write(frame)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
    break

cap.release()
out.release()

cv2.destroyAllWindows()

Upvotes: -2

Related Questions