【问题标题】:Why openCV is "blocking" my python script?为什么openCV“阻止”我的python脚本?
【发布时间】:2021-02-26 20:24:12
【问题描述】:

所以我还是 python 新手,我尝试使用一个脚本在别人写的图像上创建散景效果。这是存储库:Bokeh-Effect-in-Image-using-Python

以下是作者提供的有关代码工作原理的更多信息:bokeh-effect-in-image-using-python

请注意,我正在使用 VSCode、macOS BigSur、Python 3.9 进行编码。

首先我为我的项目创建了一个文件夹,然后我创建了一个虚拟环境,它已被 VsCode 识别并合并到项目中(所以我有带有 json 文件的 .vscode 文件夹),我创建了一个 python 文件,其中我粘贴了代码并在虚拟环境中下载了我需要的包。

我还尝试根据我在网上阅读的内容在 json 文件中添加不同的行,但它不起作用。

问题是代码运行直到遇到使用OpenCV 包的行,我没有任何 Traceback 但代码什么也不做。 这是我在终端中的内容。 Terminal

我尝试在不同的地方使用 print 并删除某些使用 opencv 的行,并且每次使用 opencv 或使用 Tracker_HSV 时都会出现问题(如果我之前使用 opencv 删除了所有行)但我认为是因为Tracker_HSV 使用了OpenCV

所以也许我在项目设置过程中犯了一个错误,或者opencv 工作不正常,我不知道该怎么办。 (对不起我的英语,我是法国人。)

【问题讨论】:

  • 您能否向我们提供您所描述问题的最小化重现?请详细描述您遇到的问题。
  • 我想我已经完全描述过了,根据我所做的不同测试,代码行似乎“阻塞了我的代码”,脚本在遇到使用 opencv 包的行时什么也不做,但它不会停止。
  • 您需要具体说明哪些代码行“阻止”了您的脚本,因为人们要求提供详细信息,您需要提供它们,而不是自我声明您描述的正确。事实并非如此。

标签: python macos opencv visual-studio-code virtualenv


【解决方案1】:

根据您提供的信息,对于模块“opencv”的使用,建议您尝试以下方法:

  1. 请正确安装模块“opencv”:“pip install opencv-python

  2. 请在此处使用代码中图片的绝对路径:“img = cv.imread("D:/.../Bokeh_Demo/flower.jpg")

  3. 在导入模块“cv2”时,因为里面包含了其他名为“cv2”的文件,(所以这里pylint报了一些错误,但这不影响代码的执行)。我们可以使用“from cv2 import cv2 as cv”来区分它们。

运行:

更新一:

请检查模块“opencv”是否已成功安装在您当前选择的VSCode环境中:

更多参考:Environment in VSCode.

更新 2:

# import cv2
from cv2 import cv2 as cv 

import numpy as np
import Tracker_HSV as tr
from PIL import Image
import os

# cv2.namedWindow('mask', cv2.WINDOW_NORMAL)
cv.namedWindow('mask',cv.WINDOW_NORMAL)

cv.resizeWindow('mask',(500,500))
filename= 'flower.jpg'
cwd = os.getcwd()
name_file=os.path.splitext(filename)[0]

path_save_temp=os.path.join(cwd,'Data')
path_save_folder=os.path.join(path_save_temp,f'{name_file}_blur_data')
if not os.path.exists(path_save_folder):
    os.makedirs(path_save_folder)


img = cv.imread("D:/....../test_Bokeh/Bokeh_Demo/flower.jpg")
blur = cv.GaussianBlur(img,(5,5),0)
img_hsv=cv.cvtColor(img,cv.COLOR_BGR2HSV)

file_save_blur= os.path.join(path_save_folder,'blur.png')
im_blur = cv.GaussianBlur(img,(81,81),0)
cv.imwrite(file_save_blur,im_blur)

xs,ys,w,h = cv.selectROI('mask',img)
crop_img=crop_img_true=crop_img_contour=img[ys:ys+h, xs:xs+w]

if not crop_img_true.shape[0]> 1:
    crop_img_true=img

x,y,z,a,b,c=(tr.tracker(crop_img_true))

crop_img_true=cv.cvtColor(crop_img_true,cv.COLOR_BGR2HSV)

file_save_mask_inrange= os.path.join(path_save_folder,'mask inRange.png')
mask_inRange=cv.inRange(crop_img_true,(x,y,z),(a,b,c))
cv.imwrite(file_save_mask_inrange,mask_inRange)


_, threshold = cv.threshold(mask_inRange, 250, 255, cv.THRESH_BINARY)
Gauss_threshold =cv.adaptiveThreshold(threshold,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY_INV,101,10)


blank_space_black= np.zeros((crop_img_true.shape[0],crop_img_true.shape[1]),np.uint8)
blank_space_black[:]=(0)

_,contours,_ = cv.findContours(Gauss_threshold, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)


maxi=cv.contourArea(contours[0])
c=[]

for cnt in contours:
    if cv.contourArea(cnt)>=maxi:
        maxi=cv.contourArea(cnt)
##        print(cv2.contourArea(cnt))
        c= cnt

file_save_contour= os.path.join(path_save_folder,'Contour.png')
cv.drawContours(crop_img_contour, c, -1, (0, 255, 0), 5)
cv.imwrite(file_save_contour,crop_img_contour)


file_save_poly= os.path.join(path_save_folder,'mask fill poly.png')
mask_poly=cv.fillConvexPoly(blank_space_black,c,(255,255,255))
cv.imwrite(file_save_poly,mask_poly)

crop_img_true=cv.cvtColor(crop_img_true,cv.COLOR_HSV2BGR)

file_save_mask_bitwise= os.path.join(path_save_folder,'mask bitwise and.png')
mask_bitwise_and = cv.bitwise_and(crop_img_true,crop_img_true,mask=mask_poly)
cv.imwrite(file_save_mask_bitwise,mask_bitwise_and)

im2= Image.open(file_save_mask_bitwise)
im2=im2.convert('RGBA')

datas=im2.getdata()
newdata=[]

for data in datas:
    if data[0]== 0 and data[1]== 0 and data[2]== 0:
        newdata.append((255,255,255,0))
    else:
        newdata.append(data)

file_save_transparent= os.path.join(path_save_folder,'transparent.png')
im2.putdata(newdata)
im2.save(file_save_transparent)

im_blur= Image.open(file_save_blur)

file_save_final= os.path.join(path_save_folder,'final.png')
im_blur.paste(im2,(xs,ys),im2)
im_blur.save(file_save_final)

im_final= Image.open(file_save_final)
im_final.show('Final Result')

cv.imshow('GaussianBlur',blur)

cv.waitKey(0)
cv.destroyAllWindows()

【讨论】:

  • 谢谢你的帮助,我试过你说的,但还是不行。我尝试使用调试器查看它“停止”的位置,并且在第一行“cv.namedWindow('mask',cv.WINDOW_NORMAL)”之后程序不会继续。也许 cv2 在 macOS Big Sur 上被“阻止”了??
  • @Mattéo Menager - 请检查模块“opencv”是否已经成功安装在你当前选择的VSCode环境中,我已经更新了我的答案,你可以参考一下。
  • 是的,我试过了,它已经安装好了,我的输出和你一模一样。
  • @Mattéo Menager - 你试过使用其他python interpreters吗?重新加载 VSCode 后会发生什么?
  • 是的,我已经尝试过,但仍然是同样的问题。
猜你喜欢
  • 1970-01-01
  • 2019-06-03
  • 1970-01-01
  • 2021-06-09
  • 1970-01-01
  • 2021-09-12
  • 2017-06-06
  • 2013-11-04
  • 2011-03-27
相关资源
最近更新 更多