Error that occurred in OpenCV3 and its solution Precautions when using OpenCV3 on Mac

problem

Let's go through all the learning with minist and do it with your favorite image! Error that occurred just before I thought (probably Mac only)

deep_learning_test.py


train_img_dirs = ['T','F']
train_image = []
train_label = []

for i, d in enumerate(train_img_dirs):
    # ./data/Get the file name in each of the following directories
    files = os.listdir('./data/' + d)
    for f in files:
        #Image loading
        img = cv2.imread('./data/' + d + '/' + f)
        img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        #One side is IMG_Resize to SIZE square
        img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
        #In a row
        img = img.flatten().astype(np.float32)/255.0
        train_image.append(img)

        # one_hot_Create a vector and add it as a label
        tmp = np.zeros(NUM_CLASSES)
        tmp[i] = 1
        train_label.append(tmp)

When I compile this ...

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in ipp_cvtColor

Fall into. why

Cause

https://stackoverflow.com/questions/20822288/python-opencv-cv2-opencv-error-assertion-failed-scn-3-scn-4-in-unkno according to When I try to print (img.shape) and the result is None type, it seems that the image cannot be read correctly. So if you do print (d) and print (f) and display the loaded directories and files

$ T
$ .DS_Store

Came out. The directory was read correctly, but the file contains an example. It wasn't running well because .DS_Store was loaded.

What is .DS_Store A hidden file for recording metadata about folder display settings such as icon position and display settings. A .DS_Store file has been created in every folder you access, both in the Finder and on the remote system.

Solution

Delete .DS_Store in separate directory

find Desktop/Target directory-name ".DS_Store" -print  -exec rm {} ";"

Delete all

sudo find / -name ".DS_Store" -delete

Will not be created in the future

defaults write com.apple.desktopservices DSDontWriteNetworkStores True

Also, you can easily erase it with an app called MacForkCleaner.

Finally

At the first OpenCV, I struggled quite a bit, wondering if the writing style or image format was wrong. (I can't find it at all even if I check it) Please refer to those who have fallen into the same phenomenon using MacOS. Since .DS_Store is a hidden file, I didn't realize that I was looking at Pat. Do other people notice it through experience?

Reference site

Delete .DS_Store from mac http://qiita.com/supersnack/items/157b69589e36f340bb29 How .DS_Store works and how to set it not to delete & create http://uxmilk.jp/48160 Python-OpenCV cv2 OpenCV Error: Assertion failed (scn == 3 || scn == 4) in unknown function, file ......\modules\imgproc\src\color.cpp https://stackoverflow.com/questions/20822288/python-opencv-cv2-opencv-error-assertion-failed-scn-3-scn-4-in-unkno

Recommended Posts

Error that occurred in OpenCV3 and its solution Precautions when using OpenCV3 on Mac
Error details and countermeasures that occurred in OpenCv2 when executing the object recognition sample program of "Object Detect on Tools"
Error and solution when installing python3 with homebrew on mac (catalina 10.15)
Error, warning when using TensorFlow on Mac
Note: When Python is run on ImageJ Fiji, java.lang.IllegalArgumentException appears and its solution (mac OS)
Precautions when using pit in Python
Precautions when using codecs and pandas
# Solution when pip install gives an error when using Anaconda on Windows 10
[Python] Error and solution memo when using venv with pyenv + anaconda
[Linux / GCP] Corrected the error that occurred when using the Git command.
I got an error when trying to install Xgboost and its solution
Precautions when using for statements in pandas
Precautions and error handling when calling .NET DLL from python using pythonnet
Video processing using Python + OpenCV on Mac
A story that an error occurred when PyInstaller was used in a program that uses googleapiclient
Precautions when using sqlite3 on macOS Sierra (10.12) with multiprocessing
I get an error when I put opencv in pyautoGUI
Error around GUI when using PyQt5 on Ubuntu (WSL2)
Solution if you crash when using selenium on heroku
How to exit when using Python in Terminal (Mac)
Minimum memo when using Python on Mac (pyenv edition)
Minimum notes when using Python on Mac (Homebrew edition)
Motion template analysis using OpenCV (recognizing an object and its moving direction in real time)
How to delete "(base)" that appears in the terminal when Anaconda is installed on Mac
Precautions when using Chainer
Building a Python environment on a Mac and using Jupyter lab
A story that stumbled when using pip in a proxy environment
Scripts that can be used when using bottle in Python
Precautions and solutions when installing Ubuntu on NVIDIA GeForce PCs
I got an error when I tried to process luigi in parallel on windows, but the solution
Precautions when drawing the probability density function and the histogram on top of each other in matplotlib