我正在使用Cygwin作为我的一个项目。我在cygwin安装过程中安装了opencv。
从上图可以看出,cycwin中已经安装了opencv 2.4版本。
但是当我在控制台中尝试导入cv2时,我收到错误"没有名为cv"的模块。
由于导入cv2不成功,我在cygdrive终端手动安装了opencv的wheel文件。但是我还没能在cygwin中访问opencv。
任何帮助都很明显。
答案 0 :(得分:1)
我想让您知道@Kathiravan_Natarajan,您的解决方案很棒,并且在成功使用之前,我已经使用过它。正如您所说,我将详细介绍必须遵循哪些步骤
使用已安装的计算机的默认python
但是,我不会更改Cygwin环境变量。我想尽可能使用Cygwin的python3
。当我需要使用cv2
时,我将简单地提供Windows安装python
的完整路径。 警告:如果您在Cygwin中使用Windows的python
并尝试向其提供文件/路径/等。在Cygwin中,您可能会遇到问题。我将在路径问题部分
好的,就在Cygwin(及其终端),我有
bballdave025@MY-MACHINE ~
$ which python3
/usr/bin/python3
bballdave025@MY-MACHINE ~
$ python3 --version
Python 3.6.9
现在,从CMD
,开始使用Windows。
C:\Windows\system32>where python
C:\Users\bballdave025\AppData\Local\Programs\Python\Python38\python.exe
C:\Users\bballdave025\AppData\Local\Programs\Python\Python38-32\python.exe
C:\Users\bballdave025\AppData\Local\Programs\Python\Python36\python.exe
C:\Users\bballdave025\AppData\Local\Microsoft\WindowsApps\python.exe
C:\Windows\system32>:: Ignore that last one - it takes you to the Microsoft Store
C:\Windows\system32>python --version
Python 3.8.2
@Kathiravan_Natarajan描述的方法的工作示例
好的。让我们得到一个不需要cv2
的快速脚本。在执行此操作时,我正在导致路径类型可能出现的问题。我们将在此处使用bash。
bballdave025@MY-MACHINE ~
$ cat > my_pwd.py<<EOF
> #!/usr/bin/python3
> # -*- coding: utf-8 -*-
> #
> import os
>
> print(os.getcwd())
> EOF
bballdave025@MY-MACHINE ~
$ chmod +x my_pwd.py
bballdave025@MY-MACHINE ~
$ # If you wanted, you could use `./my_pwd.py`, but that doesn't go to my point
bballdave025@MY-MACHINE ~
$ python3 my_pwd.py
/home/bballdave025
现在,即使这不是我们通常想要做的,我们还是使用Windows版本运行脚本。 (由于我们通常不想这样做,所以我没有将Windows版本的Python放在Cygwin的路径上。)
bballdave025@MY-MACHINE ~
$ /cygdrive/c/Users/bballdave025/AppData/Local/Programs/Python/Python38/python my_pwd.py
C:\cygwin64\home\bballdave025
如果确实要打扰您,请输入python
的长路径,请参阅第一个脚注 1 。
我建议在Cygwin中运行rm my_pwd.py
以清除不必要的文件。
希望,这使您有所了解,为什么我们不想在Cygwin中使用Windows的python
,而不是Cygwin-尽管Cygwin本身可以可能处理此路径,但是我们将在路径问题部分中对此进行讨论。
CV2和路径
现在我们已经使用了代码 without cv2
,让我们来看一下代码 with cv2
。
在Windows CMD
中,确保您拥有opencv
用于Python,并且。
python -m pip install opencv-python
现在,我将用两个脚本来说明一个bash
和一个python
,这些脚本处理一个包含五个图像的目录。
bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ ls
image_1.png image_2.png image_3.png image_4.png image_5.png
现在,为两个脚本编写代码。第一个是python
脚本,它将保持不变。第二个是Cygwin的shell脚本的问题显示(即损坏)版本。
#!/cygdrive/c/Users/bballdave025/AppData/Local/Programs/Python/Python38/python
# -*- coding: utf-8 -*-
#
#@file : show_img_and_wait.py
#
import cv2
import sys
this_img_fname = sys.argv[1]
print("Let us show {}".format(this_img_fname))
this_img = cv2.imread(this_img_fname)
cv2.imshow(this_img_fname, this_img)
cv2.waitKey(0)
print("[Trust me that {} showed up fine.]".format(this_img_fname))
print("Now, we are done showing it.")
此下一个代码仅用于显示问题。它不会工作 2 -它会给出错误并且不会显示任何内容!
#!/bin/bash
#
#@file broken_img_displays_from_path.sh
#
find /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win \
-mindepth 1 -maxdepth 1 -type f -name "*.png" | \
sort > tmp_imagepaths.txt
while read -r line; do
img_fname="${line}"
"/cygdrive/c/Users/bballdave025/AppData/Local/Programs/Python/Python38/"\
"python" -u show_img_and_wait.py "${img_fname}"
done < tmp_imagepaths.txt
rm tmp_imagepaths.txt
在Cygwin的终端上:
bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ chmod +x show_img_and_wait.py
bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ chmod +x broken_img_displays_from_path.sh
让我们首先测试python
代码。注意,由于shebang行(以#!
开头的行),我们可以运行它,并让Windows python
负责所有工作。我所显示的没有显示的那一行是为了强调一切都通过Windows运行的事实。还要记住,不会出现路径问题,因为我们在工作目录中使用的是映像。
(还请注意,尝试使用Cygwin的python
时,我们会收到一条错误消息,指出没有cv2
;运行python3 -m pip install opencv-python
至少在今天2020-05-01失败了: $ date +'%s'
=>
1588360101
。这就是OP提出问题的原因。)
bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ echo $DISPLAY
bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ ./show_img_and_wait.py image_1.png
Let us show image_1.png
[Trust me that image_1.png showed up fine.]
Now, we are done showing it.
现在,让我们尝试Cygwin的bash
脚本。
bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ ./broken_img_displays_from_path.sh
Let us show /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win/image_1.png
Traceback (most recent call last):
File "show_img_and_wait.py", line 11, in <module>
cv2.imshow(this_img_fname, this_img)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\highgui\src\wi
ndow.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in fu
nction 'cv::imshow'
Let us show /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win/image_2.png
Traceback (most recent call last):
File "show_img_and_wait.py", line 11, in <module>
cv2.imshow(this_img_fname, this_img)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\highgui\src\wi
ndow.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in fu
nction 'cv::imshow'
Let us show /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win/image_3.png
Traceback (most recent call last):
File "show_img_and_wait.py", line 11, in <module>
cv2.imshow(this_img_fname, this_img)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\highgui\src\wi
ndow.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in fu
nction 'cv::imshow'
Let us show /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win/image_4.png
Traceback (most recent call last):
File "show_img_and_wait.py", line 11, in <module>
cv2.imshow(this_img_fname, this_img)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\highgui\src\wi
ndow.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in fu
nction 'cv::imshow'
Let us show /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win/image_5.png
Traceback (most recent call last):
File "show_img_and_wait.py", line 11, in <module>
cv2.imshow(this_img_fname, this_img)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\highgui\src\wi
ndow.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in fu
nction 'cv::imshow'
这些错误有点神秘。让我们讨论发生了什么。
路径问题(和解决方案)
这里发生的是Windows python
及其版本cv2
需要Windows样式的路径。但是,如输出所示,它获得的是Cygwin版本,例如
Let us show /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win/image_1.png
Windows python
/ cv2
希望看到,例如
Let us show C:\Users\bballdave025\Desktop\testing_cv2_python_win\image_1.png
3 。
这是为什么在Cygwin终端上使用Windows python
的精巧解决方案时,我们需要跟踪路径之类的一个示例。
解决方案是将cygpath
与-w
标志一起使用。这是经过修改的bash
脚本,该脚本将允许将find
结果用于图像显示。
#!/bin/bash
#
#@file img_displays_from_path.sh
#
find /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win \
-mindepth 1 -maxdepth 1 -type f -name "*.png" | \
sort > tmp_imagepaths.txt
while read -r line; do
img_fname=$(cygpath -w "${line}")
"/cygdrive/c/Users/bballdave025/AppData/Local/Programs/Python/Python38/"\
"python" -u show_img_and_wait.py "${img_fname}"
done < tmp_imagepaths.txt
rm tmp_imagepaths.txt
回到Cygwin的终端,
bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ chmod +x img_displays_from_path.sh
bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ ./img_displays_from_path.sh
Let us show C:\Users\13852\Desktop\testing_cv2_python_win\image_1.png
[Trust me that C:\Users\13852\Desktop\testing_cv2_python_win\image_1.png showed
up fine.]
Now, we are done showing it.
Let us show C:\Users\13852\Desktop\testing_cv2_python_win\image_2.png
[Trust me that C:\Users\13852\Desktop\testing_cv2_python_win\image_2.png showed
up fine.]
Now, we are done showing it.
Let us show C:\Users\13852\Desktop\testing_cv2_python_win\image_3.png
[Trust me that C:\Users\13852\Desktop\testing_cv2_python_win\image_3.png showed
up fine.]
Now, we are done showing it.
Let us show C:\Users\13852\Desktop\testing_cv2_python_win\image_4.png
[Trust me that C:\Users\13852\Desktop\testing_cv2_python_win\image_4.png showed
up fine.]
Now, we are done showing it.
Let us show C:\Users\13852\Desktop\testing_cv2_python_win\image_5.png
[Trust me that C:\Users\13852\Desktop\testing_cv2_python_win\image_5.png showed
up fine.]
Now, we are done showing it.
成功!如果有帮助,请参考以下五张图片()。
其他路径的东西
这里是对使用Cygwin获取Windows路径可能遇到的问题的另一个小尝试。在this image
中可以看到更多(根据我自己的好奇心测验)bballdave025@MY-MACHINE ~
$ cd 'c:'
bballdave025@MY-MACHINE /cygdrive/c
$ ls 'c:'
ls: cannot access 'c:': No such file or directory
bballdave025@MY-MACHINE ~
$ cd c:
bballdave025@MY-MACHINE /cygdrive/c
$ ls 'c:\'
'$Recycle.Bin' 'Program Files' 'System Volume Information'
cygwin64 'Program Files (x86)' Users
'Documents and Settings' ProgramData Windows
nothing_important
bballdave025@MY-MACHINE ~
$ cd c:\
> # Problem; line-continuation char; I have to [Ctrl]+[C]^C
bballdave025@MY-MACHINE ~
$ cd C:\nothing_important # another one to watch out for is this one
bballdave025@MY-MACHINE /cygdrive/c/nothing_important
$ ls C:\nothing_important
ls: cannot access 'C:nothing_important': No such file or directory
bballdave025@MY-MACHINE /cygdrive/c/nothing_important
$ ls "C:\nothing_important"
nothing_at_all
bballdave025@MY-MACHINE /cygdrive/c/nothing_important
$ cd C:/
bballdave025@MY-MACHINE /cygdrive/c
$ cd nothing_important\nothing_at_all
-bash: cd: nothing_importantnothing_at_all: No such file or directory
我正在使用和扩展this answer中@mccoolive给出的示例(关于FFMPEG,但它显示了可能的问题)。
尽管实际上我们可以说处理路径的错误来自Windows python
,但我将其包括在内是为了看到Cygwin的bash
(cd
和{{1} })的Windows和Cygwin路径存在问题。
有关更多详细信息,请访问Cygwin的this documentation(archived)。
脚注
1。我使用两种方法来通过Cygwin终端更轻松地访问Windows ls
。
第一个 和我建议使用的一个是修改python
文件。我这样做如下。请注意,在更改任何内容之前,我先备份了之前的~/.bashrc
。
~/.bashrc
让我们看看它是否有效。
bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ date && date +'%s'
Fri, May 1, 2020 2:01:46 PM
1588363306
bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ cp ~/.bashrc ~/.bashrc.$(date +'%s').bak
bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ find ~ -type f -name "*.bashrc.*.bak" | tail -1
/home/bballdave025/.bashrc.1588363346.bak
bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ echo -e "\n\n# Allow access to Windows' version of python, e.g. for cv2\n"\
"alias pythonwin=/cygdrive/c/Users/bballdave025/AppData/Local/Programs/Python/Python38/python\n" >> \
~/.bashrc
bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ source ~/.bashrc
要显示第一种方法与第二种方法的缺点,我将尝试找出此bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ pythonwin --version
Python 3.8.2
bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ # That as compared to
bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ python3 --version
Python 3.6.9
bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ # and as compared to
bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ python --version
Python 2.7.16
功能的来源。使用我通常的pythonwin
无效,which pythonwin
,whereis pythonwin
和locate -b pythonwin
也无效。我没有运行它,但是apropos pythonwin
也没有得到它。实际上,我必须先进行搜索,然后才能找到获取信息的方式(我的发现1,2,3,4,5,{{ 3}},6)。可以通过find / -name "*pythonwin*"
或command -v pythonwin
找到该信息。
但是...
type pythonwin
请注意,7。
第二个 是使用符号链接。如果您要执行此操作,并且已经运行了bballdave025@MY-MACHINE ~
$ which pythonwin
which: no pythonwin in ( #the whole output of my `PATH`# )
的命令,则可以
a。恢复为备份。就我而言,应该是
alias
b。随意打开bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ cp ~/.bashrc.1588363346.bak .bashrc
bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ unalias pythonwin
,然后手动删除我们添加的行。
现在,使用符号链接。我将其放在aliases take precedence over the PATH
(user-installed programs are supposed to go)所在的文件夹中,即~/.bashrc
。我将进行设置并在下面进行检查。
/usr/local/bin/
如果您决定要删除此符号链接,请使用
bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ ln -s /cygdrive/c/Users/13852/AppData/Local/Programs/Python/Python38/python.exe /usr/local/bin/pythonwin
bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ pythonwin --version
Python 3.8.2
bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ which pythonwin
/usr/local/bin/pythonwin
bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ command -v pythonwin
/usr/local/bin/pythonwin
bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ type pythonwin
pythonwin is hashed (/usr/local/bin/pythonwin)
bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ readlink /usr/local/bin/pythonwin
/cygdrive/c/Users/13852/AppData/Local/Programs/Python/Python38/python.exe
2。的确,如果我使用bballdave025@MY-MACHINE /cygdrive/c/Users/bballdave025/Desktop/testing_cv2_python_win
$ unlink /usr/local/bin/pythonwin
作为搜索的基本目录进行搜索,这些脚本将可以完美运行。但是,除了我给出的情况以外,我还没有用标准的路径在其他疯狂的情况下,不同的路径可能失败。我认为采用规范路径的情况足以建议使用'.'
解决方案。
3。众所周知,Cygwin和Python都认为以下两个文件名字符串相同。
cygpath -w
实际上,每当我没有以编程方式查找路径时,我都会尝试使用正斜杠('/')。
答案 1 :(得分:0)
问题在于为Cygwin设置的环境变量。
一个简单的方法是:不用安装cygwin的python,就可以很容易地使用机器的默认python。它只需要设置python路径的环境变量。