所以我不知道为什么会这样。我已经使用python -m pi install -U (dependencies)
通过cmd安装了所有依赖项,在PyCharm的解释器设置中,我也已经安装了它们,并且可以清楚地看到它们已添加到项目解释器中。但是,每当我尝试将它们导入到我的代码中时,都会出现“未使用的导入语句错误”,并且该代码显示为灰色。
文件肯定在那里。我尝试第二次重新安装它们,CMD确认它们已经安装。
我正在使用最新的PyCharm内部版本(非beta版)JetBrains PyCharm Community Edition 2018.2.4 x64。和Python 3.7。我的安装目录是C:\ Python 3.7 \,在site-packages目录中,我可以清楚地看到其中的文件。
简单运动检测程序(没有在代码中实现导入的依赖项):
from __future__ import division
import cv2
from matplotlib import pyplot as plt
import numpy as np
from math import cos, sin
first_frame = None
video = cv2.VideoCapture(0)
a = 1
while True:
a = a + 1
check, frame = video.read()
print(frame)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (21, 21), 0)
if first_frame is None:
first_frame = gray
continue
delta_frame = cv2.absdiff(first_frame, gray)
thresh_delta = cv2.threshold(delta_frame, 40, 255, cv2.THRESH_BINARY)[1]
erode = cv2.erode(thresh_delta, None, iterations=1)
dilate = cv2.dilate(erode, None, iterations=1)
(_, cnts, _) = cv2.findContours(dilate.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in cnts:
if cv2.contourArea(contour) < 200:
continue
(x, y, w, h) = cv2.boundingRect(contour)
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow('captureFrame', frame)
cv2.imshow('captureGrey', gray)
cv2.imshow('delta', delta_frame)
cv2.imshow('thresh', thresh_delta)
cv2.imshow('dilate', dilate)
key = cv2.waitKey(1)
if key == ord('q'):
break
print(a)
video.release()
cv2.destroyAllWindows()