python - 无法在 RasberryPi Python 上安装 Open-CV
问题描述
我的 RasberryPi 是 V4 Python 版本:3.9.5
第一个错误代码这是在尝试使用 apt 命令安装模块后发生的
Traceback (most recent call last):
File "/home/pi/surveillance-app/main.py", line 24, in <module>
import cv2
ModuleNotFoundError: No module named 'cv2'
当前错误代码当我使用以下命令安装模块时发生这种情况
pip install --no-use-pep517 opencv-python
Traceback (most recent call last):
File "/home/pi/surveillance-app/main.py", line 24, in <module>
import cv2
File "/home/pi/.local/lib/python3.9/site-packages/cv2/__init__.py", line 5, in <module>
from .cv2 import *
ImportError: libIlmImf-2_5.so.25: cannot open shared object file: No such file or directory
我试过的
sudo apt-get install python-opencv
pip install opencv-python
pip install --no-use-pep517 opencv-python
#I tried the command below at first to install all the necessary modules and there was no error.
sudo apt install -y \
automake \
build-essential \
cmake \
libatlas3-base \
libgstreamer-plugins-base1.0-dev \
python3-opencv
源代码
# Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
import datetime
import os
import subprocess
import time
import boto3
import cv2
import dlib
KVS_STREAM_NAME = os.environ["KVS_STREAM_NAME"]
SNS_TOPIC_ARN = os.environ["SNS_TOPIC_ARN"]
KVS_PRODUCER_BUILD_PATH = os.environ["KVS_PRODUCER_BUILD_PATH"]
APP_NAME = "kvs_gstreamer_sample"
# 顔検出時に録画を行う秒数
RECORD_SEC = 30
# HLSでのセッションの有効期限(分)
EXPIRATION_MIN = 60
EXPIRATION_SEC = 60 * EXPIRATION_MIN
kvs = boto3.client("kinesisvideo")
sns = boto3.client("sns")
detector = dlib.get_frontal_face_detector()
def detect_face():
""" Wait until faces detected using Dlib """
camera = cv2.VideoCapture(0)
while True:
_, frame = camera.read()
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results, _, _ = detector.run(frame_rgb, 0)
if results:
break
camera.release()
return
def upload_video():
""" Upload video using Amazon Kinesis Video Streams Producer SDK C++ """
start = time.time()
kvs_app = f"{KVS_PRODUCER_BUILD_PATH}/{APP_NAME}"
try:
subprocess.run(
[kvs_app, KVS_STREAM_NAME],
cwd=KVS_PRODUCER_BUILD_PATH,
timeout=RECORD_SEC
)
except subprocess.TimeoutExpired:
end = time.time()
print("record finished")
return start, end
print("record interrupted")
return None, None
def get_session_url(start, end):
""" Get HLS streaming session URL """
endpoint = kvs.get_data_endpoint(
APIName="GET_HLS_STREAMING_SESSION_URL",
StreamName=KVS_STREAM_NAME
)['DataEndpoint']
kvam = boto3.client("kinesis-video-archived-media", endpoint_url=endpoint)
url = kvam.get_hls_streaming_session_url(
StreamName=KVS_STREAM_NAME,
PlaybackMode="ON_DEMAND",
ContainerFormat="MPEG_TS",
DisplayFragmentTimestamp="ALWAYS",
Expires=EXPIRATION_SEC,
HLSFragmentSelector={
"FragmentSelectorType": "PRODUCER_TIMESTAMP",
"TimestampRange": {
"StartTimestamp": start,
"EndTimestamp": end,
}
},
)['HLSStreamingSessionURL']
print(f"HLS session URL: {url}")
return url
def notify_url(url, timestamp):
""" Notify HLS streaming session URL via Amazon SNS """
date = datetime.datetime.fromtimestamp(timestamp)
subject = "通知: 顔を検出しました"
message = f"""セキュリティカメラで顔を検出しました。
ストリーム名: {KVS_STREAM_NAME}
日時: {date.strftime('%Y/%m/%d %H:%M:%S')}
再生用URL: {url} ({EXPIRATION_MIN}分のみ有効です)
"""
sns.publish(
TopicArn=SNS_TOPIC_ARN,
Message=message,
Subject=subject
)
def main():
print("start surveillance application")
while True:
detect_face()
print("face detected: start recording")
start, end = upload_video()
if start and end:
url = get_session_url(start, end)
notify_url(url, start)
if __name__ == '__main__':
main()
我试图在互联网上找到解决这个问题的方法,但我没有运气。我该如何解决这个问题?如果有人可以帮助我,我将不胜感激。
解决方案
$ sudo apt install python3-opencv
$ python3 -c "import cv2; print(cv2.__version__)"
3.2.0
$ pip3 install opencv-python
应该做的伎俩
推荐阅读
- c# - 通过 Ajax 发送从 DataTable 中挑选的对象数组到控制器 MVC 错误 400
- c - *** 检测到堆栈粉碎 *** 在双倍循环中使用双数组
- algorithm - 最多 k 个设施和预算 b 的无能力设施选址问题
- c++ - 如何创建一个类来存储大小仅受系统可用内存总量限制的数字?
- javascript - 来自输入类型=文件的角度传递引用变量
- aws-lambda - 在提供 CSS 和 JS 文件时,在 AWS Lambda 中运行的 .NET Core MVC 应用程序的身份验证不起作用
- widget - 如何获得均匀数量的网格单元
- algorithm - 堆排序(自下而上)
- reactjs - Reactjs Array.slice(开始,结束)不起作用
- swift - 如何从自定义类中的视图控制器访问变量?