Tag Archives: python

Set the maximum number of Postgres connections Error [How to Solve]

The Postgres version of this tutorial case is 12

Problem Description:

psycopg2.OperationalError: FATAL: sorry, too many clients already on

In the morning, I found that the python back-end service I maintained reported an error. Baidu found that the default maximum concurrency of Postgres was only 100, which led to this problem

Modify tutorial:

cd /var/lib/postgresql/12/main
vi postgresql.auto.conf

Add the following line of code to the file to set the maximum number of connections to 800, the default is 100
max_connections = 800

Restart the service
service postgresql restart

The following is the SQL command to check whether the modification is successful:

# Show the maximum number of connections to the current database
show max_connections;

# Show the number of users currently connected to the data
SELECT COUNT(*) from pg_stat_activity;

# Show details of the current connection, very slow, don't use it
SELECT * FROM pg_stat_activity;

The effect is similar, which indicates that the setting is successful

[Solved] JAVA Python Error: Cannot create PyString with non-byte value

Error code
case 1.
interpreter.execfile (“your file path”)
reason for error reporting: your file was not found

Situation 2. New pystring (“your string”)
error reason: there is Chinese in the string
solution: replace new pystring (“your string”) with
pystring strjason = py.newstring (“your string”)
or pystring strjason = py.newstringorunicode (“your string”);

[Mac Pro M1] Python3.9 import cv2 Error: Reason: image not found

If you forget to copy the original error message, the content is similar to:

>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File 
"/Users/xxx/Anaconda/anaconda/envs/python35/lib/python3.5/site-packages/cv2/__init__.py", line 4, in <module>
    from .cv2 import *
ImportError: dlopen(/Users/xxx/Anaconda/anaconda/envs/python35/lib/python3.5/site-packages/cv2/cv2.cpython-35m-darwin.so, 2): Library not loaded: /System/Library/Frameworks/CoreImage.framework/Versions/A/CoreImage
  Referenced from: /Users/xxx/Anaconda/anaconda/envs/python35/lib/python3.5/site-packages/cv2/.dylibs/libavcodec.57.107.100.dylib
  Reason: image not found
>>> `

The breakthrough is also in the error report, which indicates that the libavcodec library may be missing or have problems. The solution is to install the library

Specific:

$ brew install libav
$ brew install ffmpeg

[Python] error reported when reading DICOM file pydicom.errors.invaliddicomerror

[Python] error reported when reading DICOM file pydicom.errors.invaliddicomerror

When using Python to read and display DICOM files, the code is as follows:

import pydicom
import matplotlib.pyplot as plt
a = pydicom.read_file(r'C:\Users\shdn\Desktop\004.dcm')
print(a)
plt.imshow(img)
plt.show()

However, I sometimes encounter the following errors (I made the following error when reading the data given by the hospital):

The reason for this error is that the data given by the hospital may not be processed, and the file meta information header is missing, so it cannot be read directly. Solution: force reading.

The code is as follows:

import pydicom
import matplotlib.pyplot as plt
a = pydicom.read_file(r'C:\Users\shdn\Desktop\004.dcm' , force=True)
a.file_meta.TransferSyntaxUID = pydicom.uid.ImplicitVRLittleEndian
print(a)
plt.imshow(img)
plt.show()

Successfully resolved:

Pyinstaller packages the EXE file and executes the error unknown encoding: IDNA

I wrote a script and wanted to package it into an EXE file. It runs normally in the python compiler, but when packaged into an. EXE file, an error is reported. Lookuperror: unknown encoding: IDNA

Solution:
just import this module at the beginning of the script. This may be a coding problem. There is no in-depth study. Just solve it
Import encodings.idna

Opencv detection yolov4 target detection video stream error

I encountered a bug today. I didn’t have a problem before, but I have a problem today

There is no problem with yolov4 target detection image. There is a problem with this test video or calling camera detection

Report the following error:

TypeError: Expected cv::UMat for argument ‘src’

src data type = 17 is not supported

Repeated installation of OpenCV and numpy versions keeps reporting this error

Later, I thought it might be because the dataset pictures were in Chinese, so I renamed the pictures and re labeled them. The labels were also in English, and then I trained. After training, I found that there were still problems.

Finally, it is found that the result of Yolo prediction code is not directly returned to the picture,

images = yolo.detect_ image(image)
r_ image = images.get(‘image’)

The picture is in the dictionary

When the video is called

frame = np.array(yolo.detect_image(frame))

It needs to be changed to

frame = yolo.detect_ image(frame)
frame = np.array(frame.get(‘image’))

Moreover, Chinese pictures and Chinese labels have no effect.

Python reads the configuration file. Ini and resolves the error

Python reads the configuration file. Ini and reports an error raise nosectionerror (section) from none configparser. Nosectionerror: no section: ‘log’

My profile:

read profile code:
from configparser import configparser

1. Instantiate the configparser class

conf = ConfigParser()

2. Read configuration file

conf.read(“conf_ini”,encoding=‘utf-8’)

3. Read a configuration value: get, all of which are strings

Value = conf.get (‘log ‘, “name”)
error after running:
raise nosectionerror (section) from none
configparser. Nosectionerror: no section:’ log ‘
reason: the. Ini configuration file is not read
solution:
fill in the absolute path directly

An error was reported during MySQL 6.0 installation: start service failed

Existing problem:
an error is reported during MySQL 6.0 installation: start service failed, as shown in the figure


Solution:
you need to delete the relevant registry and reinstall it
1) uninstall MySQL that failed to install, uninstall MySQL in control panel → programs and functions, and delete relevant folders and files under the installation path
2) delete the MySQL related directory under the programdata directory on disk C. if it cannot be found, you need to “show hidden folder” operation
3) delete the relevant registry
(1) start → run → CMD → regedit to enter the registry list
(2) delete the following three places in sequence:

HKEY_ LOCAL_ MySQL directory under machine/system/controlset001/services/eventlog/applications; HKEY_ LOCAL_ MySQL directory under machine/system/controlset002/services/eventlog/applications; HKEY_ LOCAL_ MySQL directory under machine/system/currentcontrolset/services/eventlog/applications.

4) Restart the computer and reinstall mysql6.0.

Error when Python rarfile decompresses. Rar file: badrarfile

Python decompresses the. Rar file using the rarfile module. An error is reported:

raise BadRarFile("Failed the read enough data: req=%d got=%d" % (orig, len(data)))
rarfile.BadRarFile: Failed the read enough data: req=1151 got=52

Solution:
install the necessary modules:

pip install rarfile
pip install unrar

Download: unrar.exe
link: https://pan.baidu.com/s/1St0XHD3wMB8v6CrDsvLK4g
Extraction code: 9hr5

Put unrar.exe in the same record as the PY file you wrote, and you can successfully run and unzip the rar file.

The reason is probably:
the algorithm of RAR compression package is not open source and is not disclosed to the public. Therefore, if other software wants to compress or decompress rar files, it must call rar.exe through CMD. Therefore, it is suspected that rarfile is actually called rar.exe or unrar.exe

Reference:
unzip the rar file in Python
unzip the rar file in Python

Methods to avoid fail to allocate bitmap errors in pyplot

1. Problem recurrence

After repeated drawing (mainly creating figure ), even if only the local variables inside the function are defined, or the drawing has been closed with
PLT. Close() , pyplot still retains some contents, but the specific part has not been found yet.

The following is a bug recurrence fragment from GitHub:

import matplotlib.pyplot as plt
import numpy as np
image = np.random.randn(256, 256, 3)
image= np.array(image/image.max()*255, dtype= np.uint8)
for i in range(500):
    print(i)
    plt.cla()
    plt.imshow(image)
    plt.close()

After running, an error will be reported when I = 369 , and the memory will continue to rise before the error is reported.

memory usage change

error screenshot

2. Reason speculation

The drawing library based on Matplotlib may create window objects during drawing, but the related objects are not released when close() , resulting in continuous memory accumulation.

3. Solutions

Define a figure and axes object and reuse it. Use cla() to clear the previous drawing content before each redrawing
example 1:

import matplotlib.pyplot as plt
import numpy as np
image = np.random.randn(256, 256, 3)
image= np.array(image/image.max()*255, dtype= np.uint8)
fig, axes= plt.subplots()
for i in range(500):
    print(i)
    axes.cla()
    axes.imshow(image)

Example 2:

import matplotlib.pyplot as plt
import numpy as np

class __:
    def __init__(self):
        self.fig, self.axes= plt.subplots()

    def imshow(self, x):
        self.axes.cla()
        self.axes.imshow(image)


_= __()
image = np.random.randn(256, 256, 3)
image= np.array(image/image.max()*255, dtype= np.uint8)
for i in range(500):
    print(i)
    _.imshow(image)