python使用opennsfw2模型进行鉴黄的时候遇到ConnectionError: HTTPSConnectionPool(host='github.com', port=443)
代码:
if __name__ == '__main__':
import numpy as np
from PIL import Image
from opennsfw2._model import make_open_nsfw_model
from opennsfw2._image import preprocess_image, Preprocessing
image_path = 'E:\\temp\\sex_video\\1.png'
image = preprocess_image(Image.open(image_path), Preprocessing.YAHOO)
model = make_open_nsfw_model()
nsfw_probability = float(model.predict(np.expand_dims(image, 0), batch_size=1)[0][1])
print("sex的概率:"+nsfw_probability)
部分报错日志:
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Pre-trained weights will be downloaded.
Traceback (most recent call last):
File "E:\software\web\Anaconda3_install\envs\simswap\lib\site-packages\urllib3\connection.py", line 175, in _new_conn
(self._dns_host, self.port), self.timeout, **extra_kw
File "E:\software\web\Anaconda3_install\envs\simswap\lib\site-packages\urllib3\util\connection.py", line 95, in create_connection
raise err
File "E:\software\web\Anaconda3_install\envs\simswap\lib\site-packages\urllib3\util\connection.py", line 85, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:\software\web\Anaconda3_install\envs\simswap\lib\site-packages\urllib3\connectionpool.py", line 721, in urlopen
chunked=chunked,
File "E:\software\web\Anaconda3_install\envs\simswap\lib\site-packages\urllib3\connectionpool.py", line 403, in _make_request
self._validate_conn(conn)
File "E:\software\web\Anaconda3_install\envs\simswap\lib\site-packages\urllib3\connectionpool.py", line 1053, in _validate_conn
conn.connect()
File "E:\software\web\Anaconda3_install\envs\simswap\lib\site-packages\urllib3\connection.py", line 363, in connect
self.sock = conn = self._new_conn()
File "E:\software\web\Anaconda3_install\envs\simswap\lib\site-packages\urllib3\connection.py", line 187, in _new_conn
self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x0000021DCA5C51D0>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:\software\web\Anaconda3_install\envs\simswap\lib\site-packages\requests\adapters.py", line 450, in send
timeout=timeout
File "E:\software\web\Anaconda3_install\envs\simswap\lib\site-packages\urllib3\connectionpool.py", line 799, in urlopen
method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
File "E:\software\web\Anaconda3_install\envs\simswap\lib\site-packages\urllib3\util\retry.py", line 592, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='github.com', port=443): Max retries exceeded with url: /bhky/opennsfw2/releases/download/v0.1.0/open_nsfw_weights.h5 (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x0000021DCA5C51D0>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。',))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:\software\web\Anaconda3_install\envs\simswap\lib\site-packages\requests\adapters.py", line 519, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='github.com', port=443): Max retries exceeded with url: /bhky/opennsfw2/releases/download/v0.1.0/open_nsfw_weights.h5 (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x0000021DCA5C51D0>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。',))
原因:python代码下载文件失败,可能是网络的问题,github访问不了。
解决方案:手动下载到本地。
原来的下载地址:http://github.com/bhky/opennsfw2/releases/download/v0.1.0/open_nsfw_weights.h5
国内码云下载地址(用浏览器打开):releases/download/v0.1.0/open_nsfw_weights.h5 · zdb1314/ opennsfw2-file - Gitee.com
windows电脑下载保存的位置:C:\Users\用户名\.opennsfw2\weights
如: C:\Users\Administrator\.opennsfw2\weights\open_nsfw_weights.h5
然后就可以了,opennsfw2只要检测到本地有就不用重复下载了,解决!