# My video preview is lagging so bad, what's the problem?

**URL:** <https://forum.opencv.org/t/my-video-preview-is-lagging-so-bad-whats-the-problem/1863>\
**Category:** Python\
**Tags:** highgui\
**Created:** [February 28, 2021, 3:00pm UTC](https://forum.opencv.org/t/my-video-preview-is-lagging-so-bad-whats-the-problem/1863 "2021-02-28T15:00:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Initial\_A](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/initial_a/32/1192_2.png) [@Initial\_A](https://forum.opencv.org/u/Initial_A)\
**Post date:** [February 28, 2021, 3:00pm UTC](https://forum.opencv.org/t/my-video-preview-is-lagging-so-bad-whats-the-problem/1863/1 "2021-02-28T15:00:55Z")

</div>

Hello, I have problem that my video preview from cv2 is lagging around 2 fps even my webcam has 30 fps specification. And when I rendering my code, it needs more than 30 sec to respond. What’s the problem? My PC or anything else?

Thanks for help

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [February 28, 2021, 4:05pm UTC](https://forum.opencv.org/t/my-video-preview-is-lagging-so-bad-whats-the-problem/1863/2 "2021-02-28T16:05:52Z")

</div>

welcome.

print timestamps in your code at relevant points.

post your code, any data, the output including timestamps. also precisely describe all hardware that might be involved, certainly the make and model of your camera.

---

<div class="post-metadata">

**Author:** ![Initial\_A](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/initial_a/32/1192_2.png) [@Initial\_A](https://forum.opencv.org/u/Initial_A)\
**Post date:** [March 4, 2021, 4:03am UTC](https://forum.opencv.org/t/my-video-preview-is-lagging-so-bad-whats-the-problem/1863/3 "2021-03-04T04:03:22Z")

</div>

For my project, I use pyzbar module and this is my code

```
from cv2 import cv2
import numpy
from pyzbar.pyzbar import decode

#img = cv2.imread('12.jpg')
cap = cv2.VideoCapture(0)
cap.set(3,640)
cap.set(4,480)

while True:
    success, img = cap.read()
    for barcode in decode(img):
        print(barcode.data)
        myData = barcode.data.decode('utf-8')
        print(myData)
        pts = numpy.array([barcode.polygon],numpy.int32)
        pts = pts.reshape((-1,1,2))
        cv2.polylines(img,[pts],True,(255,0,255),5)
        pts2 = barcode.rect
        cv2.putText(img,myData,(pts2[0],pts2[1]),cv2.FONT_HERSHEY_SIMPLEX,0.9,(225,0,225),2)

    cv2.imshow("Result",img)
    cv2.waitKey(5000)

```

I use 2 camera actually in the same times. My Web camera is NYK A90 NEMESIS with specification 30 fps autofocus. there is no problem in normal use, but when I use to compile my code, i just get 1 frame per 2 sec. My PC has Intel Core i3 4th gen. Can you help me to solve this?

Thanks for helping me

---

<div class="post-metadata">

**Author:** ![matti.vuori](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@matti.vuori](https://forum.opencv.org/u/matti.vuori)\
**Post date:** [March 4, 2021, 6:45am UTC](https://forum.opencv.org/t/my-video-preview-is-lagging-so-bad-whats-the-problem/1863/4 "2021-03-04T06:45:19Z")

</div>

(You don’t compile Rython code, you just run it…)  
General debugging:

- Use you success variable for its purpose: for deciding whether to do anything  
\*Take off all barcode stuff to see if the problem is in capturing the frames of barcode decoding

…And then… Your waitKey waits 5000 ms for each frame…

---

<div class="post-metadata">

**Author:** ![Initial\_A](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/initial_a/32/1192_2.png) [@Initial\_A](https://forum.opencv.org/u/Initial_A)\
**Post date:** [March 4, 2021, 6:58am UTC](https://forum.opencv.org/t/my-video-preview-is-lagging-so-bad-whats-the-problem/1863/5 "2021-03-04T06:58:36Z")

</div>

Oh, my mistake 😅  
I dont know the purpose of waitkey actually haha. I’m newbie here so I’ll learn more. Sorry and thank you so much for your help
