Category Archives: Python

How to Solve Opencv Reads Video Error: cv2.error

Recently, due to the needs of the project, I often need to use OpenCV to read video. I often encounter a problem that an error will be reported after reading the video. Although it does not affect the code results, I really can’t stand being picky.

Error reporting procedures:

# -*-coding:utf-8-*-
"""
File Name: read_video.py
Program IDE: PyCharm
Create File By Author: Hong
"""
import cv2


def read_video(video_path: str):
    """
    OpenCV read video widget, solve the video read error problem
    :param video_path: input the path of the video file to be read
    :return: no return value
    """
    print('Video path:', video_path)
    cap = cv2.VideoCapture(video_path)
    while cap.isOpened():
        # get a frame
        ret, frame = cap.read()

        cv2.imshow("capture", frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    print('Read through the video!')
    cap.release()
    cv2.destroyAllWindows()


if __name__ == '__main__':
    path = r'D:\multi_insect_videos\multi_object00.mp4'
    read_video(path)

You can read the video normally, but the result will output an error

Traceback (most recent call last):
  File "E:/PyCharmDocument/create_ST_image/multi_insect_processing/crop_video_to_images.py", line 76, in <module>
    read_video(path)
  File "E:/PyCharmDocument/create_ST_image/multi_insect_processing/crop_video_to_images.py", line 65, in read_video
    cv2.imshow("capture", frame)
cv2.error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-uzca7qz1\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

The reason is that after the video is finished, the imshow() function of OpenCV can’t read the frame and makes an error. Solution: add judgment before imshow(). Read-only when there are frames. Exit the loop directly when there are no frames.

Error-free code:

# -*-coding:utf-8-*-
"""
File Name: read_video.py
Program IDE: PyCharm
Create File By Author: Hong
"""
import cv2


def read_video(video_path: str):
    """
    OpenCV read video widget, solve the video read error problem
    :param video_path: input the path of the video file to be read
    :return: no return value
    """
    print('Video path:', video_path)
    cap = cv2.VideoCapture(video_path)
    while cap.isOpened():
        # get a frame
        ret, frame = cap.read()
        if not ret:
            break
        cv2.imshow("capture", frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    print('Read through the video!')
    cap.release()
    cv2.destroyAllWindows()


if __name__ == '__main__':
    path = r'D:\multi_insect_videos\multi_object00.mp4'
    read_video(path)

Program output results:

Video path: D:\multi_insect_videos\multi_object00.mp4
Read through the video!

Process finished with exit code 0

[Solved] Python 3.7 from collections import Iterable Error

Python 3.7 use from collections import Iterable error

Error Message:
DeprecationWarning: Using or importing the ABCs from ‘collections’ instead of from ‘collections.abc’ is deprecated, and in 3.8 it will stop working
from collections import Iterable

Solution:
Change to from collections.abc import Iterable

Python2.7 Pyinstaller Install Error: ERROR: Command errored out with exit status 1

Python2.7 pyinstaller installation error: Command erred out with exit status 1


Error reporting reason:

Installing pyinstaller directly in python2.7 will report an error. Version 4 is incompatible with python2, so we need to specify a compatible pyinstaller version number during installation. The installation commands are as follows:

pip install pyinstaller==3.2.1

Py2exe installation command

pip install py2exe==0.6.9 

[Solved] ModuleNotFoundError: No module named ‘_polyiou‘

Problem recurrence

//install swig
sudo apt-get install swig
swig -c++ -python polyiou.i
python setup.py build_ext --inplace

//report error
ModuleNotFoundError: No module named '_polyiou'

Solution:

Swig is not installed correctly. Because there are python2 and python3 in the environment, the command to install swig is modified as follows

sudo apt-get install swig
swig -c++ -python polyiou.i
python3 setup.py build_ext --inplace

[Solved] Mujoco Error: Missing path to your environment variable.

Using mujoco to report an error: missing path to your environment variable

Question:

Recently, you want to use pycharm to run the server program in the local server environment. Follow https://www.jb51.net/article/195691.htm After step configuration, try running Import mujoco_Py , error reported:

Missing path to your environment variable.  
Current values LD_LIBRARY_PATH= 
Please add following line to .bashrc: 
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/root/.mujoco/mujoco200/bin

But I’ve added environment variables to ~ /. Bashrc

Solution:

Finally, the error reporting is solved by manually adding environment variables for the current operating environment:

In the menu bar -> Run -> Edit Configurations -> Add the corresponding environment variables to environment variables

Name Value
LD_ LIBRARY_ PATH $LD_ LIBRARY_ PATH:/root/.mujoco/mujoco200/bin

 

Gunicorn timeout error: [1] [critical] worker timeout [How to Solve]

1. Problem description

In a web project built with gunicorn + flash, because the machine learning model is used, the model needs to be loaded when requesting the interface for the first time. The model is loaded quickly when running locally, so it runs normally. The application is deployed to the server using docker (the model is mounted to the container through volume), The first request to load the model takes a long time, and the following errors are reported:

[2021-09-11 07:22:33 +0000] [1] [CRITICAL] WORKER TIMEOUT (pid:12)
[2021-09-11 07:22:35 +0000] [12] [INFO] Worker exiting (pid: 12)

Gunicorn ‘green Unicorn’ is a python WSGI HTTP server for UNIX. It is a pre forked worker model transplanted from Ruby’s Unicorn project. Gunicorn server is widely compatible with various web frameworks, with simple implementation, less use of server resources and quite fast speed.

2. Cause analysis

It can be seen from the error message that gunicorn’s worker process timed out, causing the process to exit and restart
refer to the official document of gunicorn and the description shown in the figure below:

3. Solution

From the documentation on the official website, we can know that the default timeout of gunicorn is 30s. If it exceeds 30s, the worker process will be killed and restarted.

Therefore, to solve this problem, you only need to set a large timeout: gunicorn to set the timeout

gunicorn -w 2 -b 0.0.0.0:4343  manage:app --timeout 120 

Add: -- timeout 120 to gunicorn’s startup command, indicating that the timeout is set to 120 seconds. Configure the timeout in the gunicorn. Conf.py configuration file of gunicorn

workers = 4  # Define the number of processes to be opened for processing requests at the same time, adjusted appropriately according to site traffic
worker_class = "gevent" # Use the gevent library to support asynchronous processing of requests and improve throughput
bind = "0.0.0.0:8500" # Listen to IP relaxation to facilitate communication between Dockers and between Dockers and hosts
timeout = 120 # Set the timeout to 120 seconds

For more configuration of the gunicorn. Conf.py file, see the official website

[Solved] Pycharm Use pip Error: Script file ‘D:\Anaconda3\envs\pytorch\Scripts\pip-script.py‘ is not present

Problem Description:

error reporting 1: error reporting for PIP installation: script file’d:\anaconda3\envs \ pytorch\scripts\PIP script. Py ‘is not present.
error reporting 2: PIP upgrade failed: script file’d:\anaconda3\envs\pytorch\scripts\PIP script. Py’ is not present.
as shown in the following figure:


Solution:

Baidu has had the following solutions for a long time:

# Method 1: Use the command line to go to the Anconda3\Scripts\ directory and execute the command, or if it is a virtual environment, go to the \Scripts\ directory under the virtual environment

    easy_install pip # I tried this method, but it didn't work for me 
    
# Method 2: Also go to the directory and execute the following statement

    python -m ensurepip --default-pip # Perfect solution

[Solved] RuntimeError: CUDA error: CUBLAS_STATUS_EXECUTION_FAILED when calling `cubla…

Resolve runtime error: CUDA error: cublas_STATUS_EXECUTION_FAILED when calling `cubla…

The running experiment encountered this problem. At the beginning, it was found that some people said it was because the dimensions might be different, but after inspection, this problem did not exist.

Another solution is to add a sentence of code

torch.backends.cudnn.enabled = false , but I haven’t tried yet, because it is found that the CUDA device settings of the main.py file and other files are different (there is not much data, I didn’t set nn.dataparallel, so there will be no problem after the changes are consistent.

Therefore, if you encounter this problem, you can check whether each variable and model are on the same CUDA device.

Zlib.error: error – 3 while decompressing: incorrect header check in Python

As shown in the following code:

b64_encoded_bytes = base64.b64encode(zlib.compress(b'abcde'))
encoded_bytes_representation = str(b64_encoded_bytes)  # this the cause
zlib.decompress(base64.b64decode(encoded_bytes_representation))

Zlib.decopress error

zlib.error: Error -3 while decompressing: incorrect header check

The reason for this is the use of the str() method
The description of the str() method in the python3 documentation.
If neither encoding nor errors is given, str(object) returns object.str(), which is the “informal” or nicely printable string representation of object. For string objects, this is the string itself. If object does not have a str() method, then str() falls back to returning repr(object).
The str() method only serves to put the

b'eJxLTEpOSQUABcgB8A=='

Converted to

"b'eJxLTEpOSQUABcgB8A=='"

The correct conversion should be to

"eJxLTEpOSQUABcgB8A=="

The solution is to specify the encoding method in the str() method

str(b64_encoded_bytes, 'utf-8')

[Solved] Python Error: tensorflow.python.framework.errors_impl.UnknownError: 2 root error(s) found.

environment

Environment: docker
System: Ubuntu 18.04
graphics card: RTX 1080ti
tf-v: 1.15.0

Run the neural network model and report the error of the title

The screenshot of the error is as follows:

solution:

Add the following code after the import of the entry file:

config = tf.compat.v1.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.6  # 0.6 sometimes works better for folks
keras.backend.tensorflow_backend.set_session(tf.compat.v1.Session(config=config))

Limit the running memory of Tensorflow

[Solved] AttributeError: ‘str‘ object has no attribute ‘decode‘

Original code:

def loadTxt(filenameTxt):
    txtList = [line.strip().decode('utf-8') for line in open(filenameTxt,'r').readlines()]#变成 unicode
    return txtList#unicode

report errors:

AttributeError: ‘str‘ object has no attribute ‘decode‘

Solution: decode the byte string

def loadTxt(filenameTxt):
    txtList = [line.strip().encode('utf-8').decode('utf-8') for line in open(filenameTxt,'r').readlines()]#变成 unicode
    return txtList#unicode