Я хочу передать загруженное пользователем изображение непосредственно другой функции для извлечения текста в dingo
views.py
def index(request):
# lastimage = Imageadd.objects.latest('image')
# print(lastimage)
# imagefile = lastimage.image
# image = cv2.imread(imagefile)
if request.method == 'POST' and request.FILES['pic']:
image = request.FILES['pic']
# image = cv2.imread(image)
# print(image)
de = Detect_Text(image)
# t = all_text(de)
# print(t)
obj = Imageadd(image=image)
obj.save()
return redirect('index')
return render(request,'index.html')
ex.py
def Detect_Text(image_file):
def rotate(
image: np.ndarray, angle: float, background: Union[int, Tuple[int, int, int]]
) -> np.ndarray:
old_width, old_height = image.shape[:2]
angle_radian = math.radians(angle)
width = abs(np.sin(angle_radian) * old_height) + abs(np.cos(angle_radian) * old_width)
height = abs(np.sin(angle_radian) * old_width) + abs(np.cos(angle_radian) * old_height)
image_center = tuple(np.array(image.shape[1::-1]) / 2)
rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0)
rot_mat[1, 2] += (width - old_width) / 2
rot_mat[0, 2] += (height - old_height) / 2
return cv2.warpAffine(image, rot_mat, (int(round(height)), int(round(width))), borderValue=background)
image_0 = cv2.imread(image_file)
grayscale = cv2.cvtColor(image_0, cv2.COLOR_BGR2GRAY)
resize = cv2.resize(grayscale, None, fx=0.8, fy=0.8)
angle = determine_skew(resize)
rotated = ndimage.rotate(resize, angle, reshape=True)
rotated2 = ndimage.rotate(rotated, -0.5, reshape=True)
success, encoded_image = cv2.imencode('.jpg', rotated2)
# cv2.imshow("", rotated2)
# cv2.waitKey(0)
content = encoded_image.tobytes()
image = vision.Image(content=content)
response = client.text_detection(image=image)
document = response.full_text_annotation
bounds = []
for page in document.pages:
for block in page.blocks:
for paragraph in block.paragraphs:
for word in paragraph.words:
for symbol in word.symbols:
x = symbol.bounding_box.vertices[0].x
y = symbol.bounding_box.vertices[0].y
text = symbol.text
bounds.append([x, y, text, symbol.bounding_box])
bounds.sort(key=lambda x: x[1])
old_y = -1
line = []
lines = []
threshold = 1
for bound in bounds:
x = bound[0]
y = bound[1]
if old_y == -1:
old_y = y
elif old_y - threshold <= y <= old_y + threshold:
old_y = y
else:
old_y = -1
line.sort(key=lambda x: x[0])
lines.append(line)
line = []
line.append(bound)
line.sort(key=lambda x: x[0])
lines.append(line)
return lines
Столкновение с подобной ошибкой
TypeError at /
Can't convert object to 'str' for 'filename'
Request Method: POST
Request URL: http://127.0.0.1:8000/
Django Version: 3.2.15
Exception Type: TypeError
Exception Value:
Can't convert object to 'str' for 'filename'