novakon作者
10年0个月前IP:广东
828317
21楼

blender的最大优点是可以用python进行交互。

经过一番google,我写了一组脚本,输出每一帧中无人机的屏幕坐标,作为训练用的ground truth.

get_obj_coord.py

# small helper script for CV+ML trainning ground truth data generation.
# by Qin Yongliang

import bpy
import bpy_extras

# get coordinate of object relative to screen.
def get_obj_coord(object_name):
    target = bpy.data.objects[object_name]
    
    # get object's location relative to world.
    target_loc = target.matrix_world.to_translation()
    
    cam = bpy.data.objects['Camera']
    
    scene = bpy.context.scene

    # relative to screen, where (0,0) represents left bottom of camera frame.
    co2d = bpy_extras.object_utils.world_to_camera_view(scene,cam,target_loc)

    return co2d

# get coordinate of object named 'Drone'.
def get_drone_coord():
    c = get_obj_coord('Drone')
    print('drone at screen coord:',c)
    return c

然后运行下面的脚本:

import get_obj_coord as g
import bpy

scene = bpy.context.scene
fs = scene.frame_start
fe = scene.frame_end

def get_drone_coord_list(startframe,endframe):
    list = []
    for i in range(startframe,endframe):
        scene.frame_set(i)
        c = g.get_drone_coord()
        list.append(c)
    return list
        
file = open("c:/coords.csv", "w")
list = get_drone_coord_list(fs,fe)
file.write('x,y,z\n')
for c in list:
    file.write('{},{},{}\n'.format(c.x,c.y,c.z))

file.close()

效果: blob.png

其中x和y是屏幕坐标,(0,0)是取景框左下角,(1,1)是取景框右上角。z是从相机到目标的距离。

评论
文号 / 828317

万流景仰
名片发私信
学术分 16
总主题 1270 帖总回复 8407 楼拥有证书:学者 机友 笔友
注册于 2008/03/29 15:34:00最后登录 2022/01/09 14:09:34
主体类型:个人
所属领域:无
认证方式:手机号
IP归属地:0

个人简介

已走,勿送

笔记
{{note.content}}
{{n.user.username}}
{{fromNow(n.toc)}} {{n.status === noteStatus.disabled ? "已屏蔽" : ""}} {{n.status === noteStatus.unknown ? "正在审核" : ""}} {{n.status === noteStatus.deleted ? '已删除' : ''}}
  • 编辑
  • 删除
  • {{n.status === 'disabled' ? "解除屏蔽" : "屏蔽" }}
我也是有底线的