python - 如何使用键盘快捷键自动缩进python代码
问题描述
我有以下代码:
for im_fn in tqdm(im_fns):
try:
_, fn = os.path.split(im_fn)
bfn, ext = os.path.splitext(fn)
if ext.lower() not in ['.jpg', '.png']:
continue
gt_path = os.path.join(DATA_FOLDER, "label", 'gt_' + bfn + '.txt')
img_path = os.path.join(DATA_FOLDER, "image", im_fn)
img = cv.imread(img_path)
img_size = img.shape
im_size_min = np.min(img_size[0:2])
im_size_max = np.max(img_size[0:2])
im_scale = float(600) / float(im_size_min)
if np.round(im_scale * im_size_max) > 1200:
im_scale = float(1200) / float(im_size_max)
new_h = int(img_size[0] * im_scale)
new_w = int(img_size[1] * im_scale)
new_h = new_h if new_h // 16 == 0 else (new_h // 16 + 1) * 16
new_w = new_w if new_w // 16 == 0 else (new_w // 16 + 1) * 16
re_im = cv.resize(img, (new_w, new_h), interpolation=cv.INTER_LINEAR)
re_size = re_im.shape
polys = []
with open(gt_path, 'r') as f:
lines = f.readlines()
for line in lines:
splitted_line = line.strip().lower().split(',')
x1, y1, x2, y2, x3, y3, x4, y4 = map(float, splitted_line[:8])
poly = np.array([x1, y1, x2, y2, x3, y3, x4, y4]).reshape([4, 2])
poly[:, 0] = poly[:, 0] / img_size[1] * re_size[1]
poly[:, 1] = poly[:, 1] / img_size[0] * re_size[0]
poly = orderConvex(poly)
polys.append(poly)
# cv.polylines(re_im, [poly.astype(np.int32).reshape((-1, 1, 2))], True,color=(0, 255, 0), thickness=2)
res_polys = []
for poly in polys:
# delete polys with width less than 10 pixel
if np.linalg.norm(poly[0] - poly[1]) < 10 or np.linalg.norm(poly[3] - poly[0]) < 10:
continue
res = shrink_poly(poly)
# for p in res:
# cv.polylines(re_im, [p.astype(np.int32).reshape((-1, 1, 2))], True, color=(0, 255, 0), thickness=1)
res = res.reshape([-1, 4, 2])
for r in res:
x_min = np.min(r[:, 0])
y_min = np.min(r[:, 1])
x_max = np.max(r[:, 0])
y_max = np.max(r[:, 1])
res_polys.append([x_min, y_min, x_max, y_max])
cv.imwrite(os.path.join(OUTPUT, "image", fn), re_im)
with open(os.path.join(OUTPUT, "label", bfn) + ".txt", "w") as f:
for p in res_polys:
line = ",".join(str(p[i]) for i in range(4))
f.writelines(line + "\r\n")
for p in res_polys:
cv.rectangle(re_im,(p[0],p[1]),(p[2],p[3]),color=(0,0,255),thickness=1)
cv.imshow("demo",re_im)
cv.waitKey(0)
except:
print("Error processing {}".format(im_fn))
在上面的代码中,我想删除最上面的 for 循环,try 和 except 语句。
for im_fn in tqdm(im_fns):
try:
except:
print("Error processing {}".format(im_fn))
但是,在删除它之后,我不想手动输入剩余的代码并按退格键并手动放置缩进。是否有任何键盘快捷键可以在删除 for 循环后自动缩进现有代码。
解决方案
选择所有需要缩进的代码。使用 tab 你将缩进“in”,使用 Shift+tab 你将缩进它“out”
推荐阅读
- android - Flutter:在堆栈小部件中动态添加拖放小部件
- java - 使用加速度计和陀螺仪移动鼠标光标
- bdd - 空手道是否支持 Def 变量更新最新数据
- go - google cloud的go sdk中projectslocation和projectszone功能/类型的区别
- vba - Powerpoint VBA - 防止编辑形状文本
- birt - 如何根据页面上返回和显示的数据隐藏母版页标题中的网格中的行
- python-3.x - pandoc 没有找到,long_description 不好,不要上传到 PyPI
- java - 并行运行 testMethods 而不是 testng 中的测试...?
- ssl - 变异 Webhook 不会调用端点,因为证书由未知机构签名
- php - 为卡片 php 进行分页