Home > OS >  OpenCV is not working. 'cv2' has no attribute 'gapi_wip_gst_GStreamerPipeline' (
OpenCV is not working. 'cv2' has no attribute 'gapi_wip_gst_GStreamerPipeline' (

Time:07-04

I faced this problem suddenly yesterday and before it everything is looking fine. I can't seem to find the problem. All i did was installing numpy and easyocr, and this issue appear. I have tried downgrading the opencv version and other problem appear. How could I solve this issues? I have tried most of the online solutions and they don't seem to work.

import cv2
import numpy as np
import easyocr
from nltk.corpus import words
from nltk.metrics.distance import jaccard_distance
from nltk.util import ngrams
# this needs to run only once to load the model into memory
file_name = 'Image/Image/7.jpg'

image = cv2.imread(file_name)


def opencv_resize(image, ratio):
    width = int(image.shape[1] * ratio)
    height = int(image.shape[0] * ratio)
    dim = (width, height)
    # return cv2.resize(image, dim, interpolation=cv2.INTER_AREA)
    return cv2.resize(image, dim, interpolation=cv2.INTER_NEAREST)


# def plot_rgb(image=image):
#     return cv2.imshow('rgb', cv2.cvtColor(image, cv2.COLOR_BGR2RGB))


# def plot_gray(image=image):
#     return cv2.imshow('gray', image)


# Downscale image as finding receipt contour is more efficient on a small image
resize_ratio = 500 / image.shape[0]


original = image.copy()
image = opencv_resize(image, resize_ratio)
# cv2.imshow('downscaled', image)
# Convert to grayscale for further processing
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# plot_gray(gray)
ret, img = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY, cv2.THRESH_OTSU)


# Detect all contours in Canny-edged image
contours, hierarchy = cv2.findContours(
    img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
image_with_contours = cv2.drawContours(
    image.copy(), contours, -1, (0, 255, 0), 3)
# plot_rgb(image_with_contours)


# Get 10 largest contours
largest_contours = sorted(contours, key=cv2.contourArea, reverse=True)[:10]
image_with_largest_contours = cv2.drawContours(
    image.copy(), largest_contours, -1, (0, 255, 0), 3)
# plot_rgb(image_with_largest_contours)
# approximate the contour by a more primitive polygon shape


def approximate_contour(contour):
    peri = cv2.arcLength(contour, True)
    return cv2.approxPolyDP(contour, 0.032 * peri, True)


def get_receipt_contour(contours):
    # loop over the contours
    for c in contours:
        approx = approximate_contour(c)

        # if our approximated contour has four points, we can assume it is receipt's rectangle
        # if len(approx) == 4:
        if len(approx) == 4:
            return approx


get_receipt_contour(largest_contours)

receipt_contour = get_receipt_contour(largest_contours)
image_with_receipt_contour = cv2.drawContours(
    image.copy(), [receipt_contour], -1, (0, 255, 0), 2)


def contour_to_rect(contour):
    pts = contour.reshape(4, 2)
    rect = np.zeros((4, 2), dtype="float32")
    # top-left point has the smallest sum
    # bottom-right has the largest sum
    s = pts.sum(axis=1)
    rect[0] = pts[np.argmin(s)]
    rect[2] = pts[np.argmax(s)]
    diff = np.diff(pts, axis=1)
    rect[1] = pts[np.argmin(diff)]
    rect[3] = pts[np.argmax(diff)]
    return rect / resize_ratio


def wrap_perspective(img, rect):
    # unpack rectangle points: top left, top right, bottom right, bottom left
    (tl, tr, br, bl) = rect
    # compute the width of the new image
    widthA = np.sqrt(((br[0] - bl[0]) ** 2)   ((br[1] - bl[1]) ** 2))
    widthB = np.sqrt(((tr[0] - tl[0]) ** 2)   ((tr[1] - tl[1]) ** 2))
    # compute the height of the new image
    heightA = np.sqrt(((tr[0] - br[0]) ** 2)   ((tr[1] - br[1]) ** 2))
    heightB = np.sqrt(((tl[0] - bl[0]) ** 2)   ((tl[1] - bl[1]) ** 2))
    # take the maximum of the width and height values to reach
    # our final dimensions
    maxWidth = max(int(widthA), int(widthB))
    maxHeight = max(int(heightA), int(heightB))
    # destination points which will be used to map the screen to a "scanned" view
    dst = np.array([
        [0, 0],
        [maxWidth - 1, 0],
        [maxWidth - 1, maxHeight - 1],
        [0, maxHeight - 1]], dtype="float32")
    M = cv2.getPerspectiveTransform(rect, dst)
    return cv2.warpPerspective(img, M, (maxWidth, maxHeight))


scanned = wrap_perspective(original.copy(), contour_to_rect(receipt_contour))


def bw_scanner(image):
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    return gray


result = bw_scanner(scanned)



correct_words = words.words()


def wordDetectionAndExtraction(img):
    reader = easyocr.Reader(['en'])
    readImage = reader.readtext(img, detail=0)
    return readImage

print(wordDetectionAndExtraction(result))
Traceback (most recent call last):

    return _bootstrap._gcd_import(name[level:], package, level)
  File "C:\Users\user\AppData\Roaming\Python\Python310\site-packages\cv2\gapi\__init__.py", line 290, in <module>
    cv.gapi.wip.GStreamerPipeline = cv.gapi_wip_gst_GStreamerPipeline
AttributeError: partially initialized module 'cv2' has no attribute 'gapi_wip_gst_GStreamerPipeline' (most likely due to a circular import)

CodePudding user response:

Try This :

pip install "opencv-python-headless<4.3"

It solved my issue

CodePudding user response:

You have mentioned it rightly, this can be because of multiple versions of opencv being installed. Check the versions of opencv installed

pip list | grep opencv

If it throws 2 opencv versions installed, go ahead and remove one

  • Related