# Could not collect images for labeling and training

**URL:** <https://forum.opencv.org/t/could-not-collect-images-for-labeling-and-training/1728>\
**Category:** Python\
**Created:** [February 23, 2021, 8:58am UTC](https://forum.opencv.org/t/could-not-collect-images-for-labeling-and-training/1728 "2021-02-23T08:58:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ren\_Ngullie](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/ren_ngullie/32/1105_2.png) [@Ren\_Ngullie](https://forum.opencv.org/u/Ren_Ngullie)\
**Post date:** [February 23, 2021, 8:58am UTC](https://forum.opencv.org/t/could-not-collect-images-for-labeling-and-training/1728/1 "2021-02-23T08:58:54Z")

</div>

`\>  
the code

```
  import cv2 
  import os
  import time
  import uuid

  IMAGES_PATH = 'Tensorflow/workspace/images/collectedimages'
  labels = ['hello', 'thanks', 'yes', 'no', 'IloveYou']
   number_imgs = 15

 for label in labels:
!mkdir {'Tensorflow\workspace\images\collectedimages\\'+label}
cap = cv2.VideoCapture(0)
print('collecting image for {}'.format(label))
time.sleep(2)
for imgnum in range(number_imgs): 
    ret, frame = cap.read()
    imgname = os.path.join(IMAGES_PATH, label, label+'.'+'{}.jpeg'.format(str(uuid.uuid1())))
    cv2.imwrite(imgname, frame)
    cv2.imshow('frame', frame)
    time.sleep(5)
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    cap.release()

```

```auto
the error

```

```
error Traceback (most recent call last)
<ipython-input-6-8ea005f6434e> in <module>
        7 ret, frame = cap.read()
        8 imgname = os.path.join(IMAGES_PATH, label, label+'.'+'{}.jpeg'.format(str(uuid.uuid1())))
  ----> 9 cv2.imwrite(imgname, frame)
        10 cv2.imshow('frame', frame)
        11 time.sleep(5)

error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-i1s8y2i1\ opencv\modules\imgcodecs\src\loadsave.cpp:753: error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite'

```

```auto

real time object detection project
```

---

<div class="post-metadata">

**Author:** ![DoXXXX](https://avatars.discourse-cdn.com/v4/letter/d/aca169/32.png) [@DoXXXX](https://forum.opencv.org/u/DoXXXX)\
**Post date:** [February 23, 2021, 10:08am UTC](https://forum.opencv.org/t/could-not-collect-images-for-labeling-and-training/1728/2 "2021-02-23T10:08:47Z")

</div>

The error shows your image writed by cv2.imwrite is empty and cv2.VideoCapture(0) caputres the image from the first camera device. Please check the camera run normally and make sure the image captured by camera is correct.

---

<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 23, 2021, 10:58am UTC](https://forum.opencv.org/t/could-not-collect-images-for-labeling-and-training/1728/3 "2021-02-23T10:58:08Z")

</div>

> [@Ren\_Ngullie](#):
>
> ```auto
> ret, frame = cap.read()
> 
> ```

ALWAYS check if `ret` is True:

```python
if not ret:
    break

```

that would have avoided the error.

oh, and your code will still not do what you want. `time.sleep(5)` is useless because the camera will still produce frames, which will be _stale_ by the time you read them.

---

<div class="post-metadata">

**Author:** ![Ren\_Ngullie](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/ren_ngullie/32/1105_2.png) [@Ren\_Ngullie](https://forum.opencv.org/u/Ren_Ngullie)\
**Post date:** [February 24, 2021, 4:03am UTC](https://forum.opencv.org/t/could-not-collect-images-for-labeling-and-training/1728/4 "2021-02-24T04:03:43Z")

</div>

Thank you for replying…well it capture one image into the assigned folder then the pop up which capture the image goes to not responding …could it be a hardware issue cause Im using my laptops webcam?

---

<div class="post-metadata">

**Author:** ![DoXXXX](https://avatars.discourse-cdn.com/v4/letter/d/aca169/32.png) [@DoXXXX](https://forum.opencv.org/u/DoXXXX)\
**Post date:** [February 24, 2021, 10:31am UTC](https://forum.opencv.org/t/could-not-collect-images-for-labeling-and-training/1728/5 "2021-02-24T10:31:50Z")

</div>

Please check cap.release() which is in the for loop and VideoCapture will be released by it and not capture image anymore. Moreover, cv2.waitKey(1) code can delay the video capture stream. It’s no need to use time.sleep to delay.

---

<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 24, 2021, 3:40pm UTC](https://forum.opencv.org/t/could-not-collect-images-for-labeling-and-training/1728/6 "2021-02-24T15:40:19Z")

</div>

> [@DoXXXX](#):
>
> cv2.waitKey(1) code can delay the video capture stream.

it can’t. the camera will produce frames regardless. when you wait, say 5 seconds, no matter how, you will get the next frame, which will be 5 seconds old, i.e. “stale”.

waitKey() is preferable to sleep because it doesn’t cause the GUI to lock up.

---

<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:** [September 5, 2023, 10:01pm UTC](https://forum.opencv.org/t/could-not-collect-images-for-labeling-and-training/1728/8 "2023-09-05T22:01:17Z")

</div>

A post was split to a new topic: [!\_img.empty() in function ‘cv::imwrite’, (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support](https://forum.opencv.org/t/img-empty-in-function-cv-imwrite-2-unspecified-error-the-function-is-not-implemented-rebuild-the-library-with-windows-gtk-2-x-or-cocoa-support/14449)
