如何使用python将选定部分的坐标存储为列表?
我想将选定的感兴趣的矩形区域的坐标存储为一个列表,这样我就可以迭代坐标并从类似的文档中提取信息 现在,我可以使用鼠标选择感兴趣的矩形区域,然后,如果我按“回车”键,则可以继续标记。如果按下'Q',它将打印最后选择的感兴趣区域的坐标 这是我的密码如何使用python将选定部分的坐标存储为列表?,python,python-3.x,list,opencv,computer-vision,Python,Python 3.x,List,Opencv,Computer Vision,我想将选定的感兴趣的矩形区域的坐标存储为一个列表,这样我就可以迭代坐标并从类似的文档中提取信息 现在,我可以使用鼠标选择感兴趣的矩形区域,然后,如果我按“回车”键,则可以继续标记。如果按下'Q',它将打印最后选择的感兴趣区域的坐标 这是我的密码 import cv2 import pytesseract coordinates = [] # Defining the event listener (callback function) def shape_selection(e
import cv2
import pytesseract
coordinates = []
# Defining the event listener (callback function)
def shape_selection(event, x, y, flags, param):
# making coordinates global
global coordinates
# Storing the (x1,y1) coordinates when left mouse button is pressed
if event == cv2.EVENT_LBUTTONDOWN:
coordinates = [(x, y)]
# Storing the (x2,y2) coordinates when the left mouse button is released and make a rectangle on the selected region
elif event == cv2.EVENT_LBUTTONUP:
coordinates.append((x, y))
# Drawing a rectangle around the region of interest (roi)
cv2.rectangle(image, coordinates[0], coordinates[1], (0,0,255), 2)
cv2.imshow("image", image)
# load the image, clone it, and setup the mouse callback function
image = cv2.imread(r'C:\Users\User\Desktop\ocr template\Sample_Invoice.jpg')
image = cv2.resize(image,(1000,1000))
image_copy = image.copy()
cv2.namedWindow("image")
cv2.setMouseCallback("image", shape_selection)
f = open(r'C:\Users\User\Desktop\ocr template\data.txt', "a")
# keep looping until the 'q' key is pressed
while True:
# display the image and wait for a keypress
cv2.imshow("image", image)
key = cv2.waitKey(1) & 0xFF
if key==13:
image_roi = image_copy[coordinates[0][1]:coordinates[1][1],
coordinates[0][0]:coordinates[1][0]]
text = pytesseract.image_to_string(image_roi).replace(',', ' ').replace('\f', '')
print(text)
f.write(text + '\n')
if key == ord("c"):
image = image_copy.copy()
if key == ord("q"):
f.close()
break
if len(coordinates) == 2:
image_roi = image_copy[coordinates[0][1]:coordinates[1][1],
coordinates[0][0]:coordinates[1][0]]
cv2.imshow("Selected Region of Interest - Press any key to proceed", image_roi)
cv2.waitKey(0)
# closing all open windows
cv2.destroyAllWindows()
为了将所选部分的坐标存储为列表,我应该对代码做什么更改?您已经完成了最难的部分。 假设坐标变量具有矩形的四个角,可以将它们直接附加到结果列表中
result_list.append(coordinates)
或者,您可以使用namedtuple创建一个矩形
from collections import namedtuple
Rectangle = namedtuple('Rectangle', 'x1 x2 y1 y2')
rectangle_1 = Rectangle(coordinates[0][1],coordinates[1][1],coordinates[0][0],coordinates[1][0])
result_list.append(rectangle_1)
您可以访问矩形_1.x1、矩形_1.x2等坐标
如果要保存此列表以供以后使用,可以使用内置模块
你已经完成了困难的部分。 假设坐标变量具有矩形的四个角,可以将它们直接附加到结果列表中
result_list.append(coordinates)
或者,您可以使用namedtuple创建一个矩形
from collections import namedtuple
Rectangle = namedtuple('Rectangle', 'x1 x2 y1 y2')
rectangle_1 = Rectangle(coordinates[0][1],coordinates[1][1],coordinates[0][0],coordinates[1][0])
result_list.append(rectangle_1)
您可以访问矩形_1.x1、矩形_1.x2等坐标
如果要保存此列表以供以后使用,可以使用内置模块
你已经完成了困难的部分,所以我不知道你在问什么?您想将结果放在列表中供以后使用还是仅用于此运行周期?我想存储使用矩形工具选择的坐标,该工具是我为选择感兴趣的区域而创建的,以便我可以从类似的文档中提取相同的信息。我的答案将适用于您。我还需要什么不在里面的东西吗?我现在可以用命名的元组得到坐标列表了。非常感谢兄弟。还有一个疑问,我得到了..例如,
[矩形(x1=57,x2=124,y1=187,y2=694)]
作为选择部分时的列表。如何将存储的坐标传递给新文档我不知道如何调用另一个文档进行处理。但是您可以像传递任何其他参数一样传递这个矩形参数。你能告诉我你从哪里开始处理另一个新文档的代码吗?你已经完成了最难的部分,所以我不知道你在问什么?您想将结果放在列表中供以后使用还是仅用于此运行周期?我想存储使用矩形工具选择的坐标,该工具是我为选择感兴趣的区域而创建的,以便我可以从类似的文档中提取相同的信息。我的答案将适用于您。我还需要什么不在里面的东西吗?我现在可以用命名的元组得到坐标列表了。非常感谢兄弟。还有一个疑问,我得到了..例如,[矩形(x1=57,x2=124,y1=187,y2=694)]
作为选择部分时的列表。如何将存储的坐标传递给新文档我不知道如何调用另一个文档进行处理。但是您可以像传递任何其他参数一样传递这个矩形参数。能否显示开始处理另一个新文档的代码?