我已经在 labelImg 工具中对图像进行了注释,并以 XML 形式获得了注释。我需要将其转换为 LabelMe JSON 格式,并在其中编码 imageData。
样本输入:
示例 XML:
<annotation>
<folder>blocks</folder>
<filename>sample_annotation.jpg</filename>
<path>/path/sample_annotation.jpg</path>
<source>
<database>Unknown</database>
</source>
<size>
<width>720</width>
<height>540</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>cube</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>90</xmin>
<ymin>87</ymin>
<xmax>196</xmax>
<ymax>194</ymax>
</bndbox>
</object>
<object>
<name>cube</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>498</xmin>
<ymin>188</ymin>
<xmax>607</xmax>
<ymax>296</ymax>
</bndbox>
</object>
</annotation>Run Code Online (Sandbox Code Playgroud)
所需的示例输出:
{'imageData': '/9j/2w.........../9k=',
'imageHeight': 540,
'imagePath': 'sample_annotation.jpg',
'imageWidth': 720,
'shapes': [{'group_id': None,
'label': 'cube',
'points': [[90, 87], [196, 194]],
'shape_type': 'rectangle'},
{'group_id': None,
'label': 'cube',
'points': [[498, 188], [607, 296]], …Run Code Online (Sandbox Code Playgroud) 我有以下数据框:
df = pd.DataFrame({
'fruit':
['peaches']*5 + ['apples']*5 + ['bananas']*3 +
['nectarines']*3 + ['carrots']*3 + ['apricots']
})
Run Code Online (Sandbox Code Playgroud)
我想先按值计数对输出进行排序,然后按水果名称的字母顺序排序:
apples 5
peaches 5
bananas 3
carrots 3
nectarines 3
apricots 1
Run Code Online (Sandbox Code Playgroud)
我找到了这个答案,但它看起来已经过时了。
想要计算熊猫日期系列之间的天数差 -
0 2013-02-16
1 2013-01-29
2 2013-02-21
3 2013-02-22
4 2013-03-01
5 2013-03-14
6 2013-03-18
7 2013-03-21
Run Code Online (Sandbox Code Playgroud)
和今天的日期。
我尝试过,但无法提出合乎逻辑的解决方案。请帮我写代码。实际上我是 python 的新手,在应用任何函数时都会发生很多语法错误。
我想获取第二个矩形顶部的粗体黑色文本以及三个彩色矩形的 (x,y) 坐标。
我已经准备好了面具,除了我无法弄清楚的黑色文本的面具。但是,文本始终位于矩形的顶部,因此如果我能够找出底部大矩形的位置,我也会得到文本的位置。
我尝试根据此评论使用 ConnectedComponents 函数使用 ConnectedComponents 函数,但除了对各种对象进行着色和分组之外,我无法继续前进,因此我没有在下面包含该代码片段以使事情尽可能清晰。
到目前为止,这是我的代码:
import cv2
import numpy as np
import imutils
PATH = "stackoverflow.png"
img = cv2.imread(PATH)
imgHSV = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
mask_border = cv2.inRange(imgHSV,np.array([0,0,170]),np.array([0,0,175]))
mask_green = cv2.inRange(imgHSV,np.array([76,221,167]),np.array([76,221,167]))
mask_pink = cv2.inRange(imgHSV,np.array([168,41,245]),np.array([172,41,252]))
mask_red = cv2.inRange(imgHSV,np.array([4,207,251]),np.array([4,207,251]))
#mask_black = ???
all_masks = cv2.bitwise_or(mask_border, mask_green)
all_masks = cv2.bitwise_or(all_masks, mask_pink)
all_masks = cv2.bitwise_or(all_masks, mask_red)
cv2.imshow("Masks", all_masks)
imgResult = cv2.bitwise_and(img,img,mask=all_masks)
cv2.imshow("Output", imgResult)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud) 我有一个相当深刻的字典需要简化。我在这样做时遇到了一些问题。
这是需要简化的字典的一个小样本:
data_dict = {
"DATA": {
"Page1": [{
"Section": [{
"Name": [{
"text": "John"
}],
"ID_Number": [{
"text": "123456"
}]
}]
}],
"Page2": [{
"Section": [{
"Name": [{
"text": "Rob"
}],
"ID_Number": [{
"text": "654321"
}]
}]
}]
}
}
Run Code Online (Sandbox Code Playgroud)
我已经做了什么:
my_dict = {}
for value in data_dict.values():
for key, val in value.items():
if "Tab" in key:
my_dict[key] = val
if type(val) == list:
for i in val:
for key1, val1 in i.items():
my_dict[key] = val1
result_dict = {} …Run Code Online (Sandbox Code Playgroud) 我一直在尝试检测复选框。虽然我能够检测到其他图像中的方形轮廓,但我无法获取该特定图像的轮廓。请帮我检测复选框。
这是我的代码,
for myfile in files:
image=cv2.imread(myfile)
image = cv2.resize(image, (180,60), interpolation = cv2.INTER_AREA)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#apply otsu's threshold
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
#setting up threshold values
threshold_max_area = 300
threshold_min_area = 10
#finding contours in the image
cnts = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
#getting the coordinates for each checkbox
count=0
centers=[]
for c in cnts:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.035 * …Run Code Online (Sandbox Code Playgroud) 我有以下数据框架.
ID Product quantity
9626 a 1
9626 b 1
9626 c 1
6600 f 1
6600 a 1
6600 d 1
Run Code Online (Sandbox Code Playgroud)
我希望通过ID加入行.以下是结果的示例.(数量列是可选的.此列不是必需的.)
ID Product quantity
9626 a,b,c 3
6600 a,d,f 3
Run Code Online (Sandbox Code Playgroud)
我使用了合并和求和,但它没有用.
这个问题只用循环语句解决了吗?
如果您能为我提供解决方案,我将不胜感激.
我正在使用该duplicated函数检查 panadas 数据帧中的重复行,该函数运行良好。但是如何仅打印出真实项目的行内容?
例如,如果我运行:
duplicateCheck = dataSet.duplicated(subset=['Name', 'Date',], keep=False)
print(duplicateCheck)
Run Code Online (Sandbox Code Playgroud)
它输出:
0 False
1 False
2 False
3 False
4 True
5 True
6 False
7 False
8 False
9 False
Run Code Online (Sandbox Code Playgroud)
我正在寻找类似的东西:
for row in duplicateCheck.keys():
if row == True:
print (row, duplicateCheck[row])
Run Code Online (Sandbox Code Playgroud)
它打印数据框中重复的项目。