"ModelScope中,代码修改后是下面的:
""""""
@author: MrJson
@time: 2024/5/8 10:32
@describe: TODO
""""""
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
import numpy as np
import cv2
import math
import pandas as pd
ocr_recognition = pipeline(Tasks.ocr_recognition, model='damo/cv_convnextTiny_ocr-recognition-general_damo')
ocr_detection = pipeline(Tasks.ocr_detection, model='damo/cv_resnet18_ocr-detection-line-level_damo')
def crop_image(img, position):
def distance(x1,y1,x2,y2):
return math.sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2))
position = position.tolist()
for i in range(4):
for j in range(i+1, 4):
if(position[i][0] > position[j][0]):
tmp = position[j]
position[j] = position[i]
position[i] = tmp
if position[0][1] > position[1][1]:
tmp = position[0]
position[0] = position[1]
position[1] = tmp
if position[2][1] > position[3][1]:
tmp = position[2]
position[2] = position[3]
position[3] = tmp
x1, y1 = position[0][0], position[0][1]
x2, y2 = position[2][0], position[2][1]
x3, y3 = position[3][0], position[3][1]
x4, y4 = position[1][0], position[1][1]
corners = np.zeros((4,2), np.float32)
corners[0] = [x1, y1]
corners[1] = [x2, y2]
corners[2] = [x4, y4]
corners[3] = [x3, y3]
img_width = distance((x1+x4)/2, (y1+y4)/2, (x2+x3)/2, (y2+y3)/2)
img_height = distance((x1+x2)/2, (y1+y2)/2, (x4+x3)/2, (y4+y3)/2)
corners_trans = np.zeros((4,2), np.float32)
corners_trans[0] = [0, 0]
corners_trans[1] = [img_width - 1, 0]
corners_trans[2] = [0, img_height - 1]
corners_trans[3] = [img_width - 1, img_height - 1]
transform = cv2.getPerspectiveTransform(corners, corners_trans)
dst = cv2.warpPerspective(img, transform, (int(img_width), int(img_height)))
return dst
def orderpoint(coor):
arr = np.array(coor).reshape([4, 2])
sum = np.sum(arr, 0)
centroid = sum_ / arr.shape[0]
theta = np.arctan2(arr[:, 1] - centroid[1], arr[:, 0] - centroid[0])
sort_points = arr[np.argsort(theta)]
sort_points = sort_points.reshape([4, -1])
if sort_points[0][0] > centroid[0]:
sort_points = np.concatenate([sort_points[3:], sort_points[:3]])
sort_points = sort_points.reshape([4, 2]).astype('float32')
return sort_points
def text_detection(image_full, ocr_detection):
det_result = ocr_detection(image_full)
det_result = det_result['polygons']
# sort detection result with coord
det_result_list = det_result.tolist()
det_result_list = sorted(det_result_list, key=lambda x: 0.01*sum(x[::2])/4+sum(x[1::2])/4)
return np.array(det_result_list)
def text_recognition(det_result, image_full, ocr_recognition):
output = []
for i in range(det_result.shape[0]):
pts = order_point(det_result[i])
image_crop = crop_image(image_full, pts)
result = ocr_recognition(image_crop)
output.append([str(i+1), result['text'], ','.join([str(e) for e in list(pts.reshape(-1))])])
result = pd.DataFrame(output, columns=['检测框序号', '行识别结果', '检测框坐标'])
return result
image_full = ""images_0.png""
det_result = text_detection(image_full, ocr_detection)
ocr_result = text_recognition(det_result, image_full, ocr_recognition)
print(ocr_result)
执行后报错:
2024-05-08 10:43:09.568420: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 67108864 exceeds 10% of system memory.
2024-05-08 10:43:09.697486: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 67634176 exceeds 10% of system memory.
2024-05-08 10:43:10.341440: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 67108864 exceeds 10% of system memory.
2024-05-08 10:43:10.404067: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 68161536 exceeds 10% of system memory.
2024-05-08 10:43:10.745958: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 67108864 exceeds 10% of system memory.
Traceback (most recent call last):
File ""test02.py"", line 93, in
ocr_result = text_recognition(det_result, image_full, ocr_recognition)
File ""test02.py"", line 85, in text_recognition
image_crop = crop_image(image_full, pts)
File ""test02.py"", line 59, in crop_image
dst = cv2.warpPerspective(img, transform, (int(img_width), int(img_height)))
cv2.error: OpenCV(4.8.0) :-1: error: (-5:Bad argument) in function 'warpPerspective'
Overload resolution failed:
- src is not a numpy array, neither a scalar
- Expected Ptr for argument 'src'
ModelScope中执行环境linux cpu 这个哪里出现问题了?是代码问题还是镜像问题呢?"
ModelScope旨在打造下一代开源的模型即服务共享平台,为泛AI开发者提供灵活、易用、低成本的一站式模型服务产品,让模型应用更简单!欢迎加入技术交流群:微信公众号:魔搭ModelScope社区,钉钉群号:44837352