# Image matching with real-time video

**URL:** <https://forum.opencv.org/t/image-matching-with-real-time-video/3289>\
**Category:** Python\
**Created:** [May 11, 2021, 11:02am UTC](https://forum.opencv.org/t/image-matching-with-real-time-video/3289 "2021-05-11T11:02:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jophin123](https://avatars.discourse-cdn.com/v4/letter/j/50afbb/32.png) [@jophin123](https://forum.opencv.org/u/jophin123)\
**Post date:** [May 11, 2021, 11:02am UTC](https://forum.opencv.org/t/image-matching-with-real-time-video/3289/1 "2021-05-11T11:02:18Z")

</div>

Hello guys I want to make a python project in which an image is matching with a frame in real-time video capturing.But I can’t get the code .Anyone can help me with it

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [May 11, 2021, 11:11am UTC](https://forum.opencv.org/t/image-matching-with-real-time-video/3289/2 "2021-05-11T11:11:50Z")

</div>

it’s nice, that you want this, but we can only help with YOUR OWN EFFORT, we won’t write your program.

> [@jophin123](#):
>
> project in which an image is matching with a frame in real-time video capturing.

this is also far too broad as a description. do some research, find out, what you really want/need (image matching ? object detection ? classification of something ?)

once you can refine your needs, and have any attempt to show, we can help you with it (but not as it is now, sorry)

> [@jophin123](#):
>
> But I can’t get the code

it’s about understanding things here, not “getting the code”

---

<div class="post-metadata">

**Author:** ![jophin123](https://avatars.discourse-cdn.com/v4/letter/j/50afbb/32.png) [@jophin123](https://forum.opencv.org/u/jophin123)\
**Post date:** [May 11, 2021, 4:41pm UTC](https://forum.opencv.org/t/image-matching-with-real-time-video/3289/3 "2021-05-11T16:41:13Z")

</div>

import cv2  
from matplotlib import pyplot as plt  
import numpy as np

cap = cv2.VideoCapture(0) #Webcam Capture  
while(True):

```
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

for i in range(3):
	template = cv2.imread(r'C:\Users\hp\Desktop\face'+str(i)+'.jpg',0)
	#path = r'C:\Users\hp\Desktop\face\jop2.jpg'
    

	#template = cv2.imread(path,0)
    
	w, h = template.shape[::-1]

	res = cv2.matchTemplate(gray,template,cv2.TM_SQDIFF)

	min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

	top_left = min_loc
	bottom_right = (top_left[0] + w, top_left[1] + h)

	cv2.rectangle(frame,top_left, bottom_right, 255, 1)
	cv2.putText(frame, 'Detected Face ID: '+str(i), (top_left[0],top_left[1]-10), 
			cv2.FONT_HERSHEY_PLAIN, 1.0, (255,255,255))

cv2.imshow('Test',frame)

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

```

cap.release()  
cv2.destroyAllWindows()

THIS ISTHE CODE BUT IT SHOWS THE ERROR AttributeError: ‘NoneType’ object has no attribute ‘shape’.

ICHECKED THE PATH ITS CORRECT BUTI THINK THERE IS ERROR WITH THIS ONE  
template = cv2.imread(r’C:\Users\hp\Desktop\face’+str(i)+’.jpg’,0)

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [May 11, 2021, 4:46pm UTC](https://forum.opencv.org/t/image-matching-with-real-time-video/3289/4 "2021-05-11T16:46:31Z")

</div>

> [@jophin123](#):
>
> ‘NoneType’ object has no attribute ‘shape’.

please make it a habit to _check_ the output from imread() or cap.read() like:

```
if (np.shape(img)==())
     break # read fail, invalid data

```
