def build(src_file): savedpath = os.getcwd() try: os.chdir('data') return OBJ('%s.obj' % meshname) finally: os.chdir(savedpath)
def readOBJ(file_name): obj = OBJ(file_name) # print(obj.mtl) for name, mtl in obj.mtl.items(): if 'texture_Kd' in mtl: return mtl['texture_Kd'] return ''
def init_gl(): global obj global cam global sun global mercury, venus, earth, mars, jupiter, saturn, uranus, neptune global planets global asteroid global t global quad global ship glClearColor(0.0, 0.0, 0.0, 1.0) glClearDepth(1.0) glEnable(GL_DEPTH_TEST) glShadeModel(GL_SMOOTH) glEnable(GL_LIGHTING) glLightfv(GL_LIGHT0, GL_AMBIENT, light_amb_0) glLightfv(GL_LIGHT0, GL_POSITION, light_pos_0) glEnable(GL_LIGHT0) glLightfv(GL_LIGHT1, GL_POSITION, light_pos_1) glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, light_dir_1) glLightfv(GL_LIGHT1, GL_AMBIENT, light_amb_1) glLightfv(GL_LIGHT1, GL_DIFFUSE, light_dif_1) glEnable(GL_LIGHT1) obj = OBJ("cube.obj", textureFile="stars.jpg") obj.generate() cam = Camera() sun = Sun(textureFile="sun.jpg") mercury = Planet("mercury.jpg", 8, 5, 0.5, 0.1, 0.25) venus = Planet("venus.jpg", 9.6, 6, 0.4, 0.1, 0.35) earth = Planet("earth.jpg", 11.2, 7, 0.3, 0.1, 0.45, "moon.jpg", [0.7, 0.8, 0.09]) mars = Planet("mars.jpg", 12.8, 8, 0.2, 0.1, 0.33) jupiter = Planet("jupiter.jpg", 19.2, 12, 0.2, 1, 1.5, "jupiter-moon.jpg", [3, 1, 0.3]) saturn = Planet("saturn.jpg", 25.6, 16, 0.15, 0.2, 1.1) uranus = Planet("uranus.jpg", 30.4, 19, 0.11, 0.2, 0.8) neptune = Planet("neptune.jpg", 32.8, 20.5, 0.09, 0.2, 0.7) planets = [mercury, venus, earth, mars, jupiter, saturn, uranus, neptune] quad = gluNewQuadric() gluQuadricTexture(quad, GL_TRUE) asteroid = Asteroid(Fire()) t = Texture("rings.jpg") ship = pywavefront.Wavefront('Apollo/apollo.obj')
def InitGL(self): self.safe_angle = list(self.angle) #the last angle ran self.detect_col = True #collision self.port_locked = False #serial port lock self.project = self.frame.project #main project #light 0 glLightfv(GL_LIGHT0, GL_POSITION, (-100, 500, 0, 1.0)) glLightfv(GL_LIGHT0, GL_AMBIENT, (0.2, 0.2, 0.2, 1.0)) glLightfv(GL_LIGHT0, GL_DIFFUSE, (.1, .1, .1, .1)) glLightModelfv(GL_LIGHT_MODEL_AMBIENT, [0.2, 0.2, 0.2, 1.0]) glEnable(GL_LIGHT0) #Light 1 glLightfv(GL_LIGHT1, GL_AMBIENT, [0.0, 0.0, 0.0, 1.0]) # R G B A glLightfv(GL_LIGHT1, GL_DIFFUSE, [.85, .85, .85, 1]) # R G B A glLightfv(GL_LIGHT1, GL_POSITION, [300, 550, 300, 0.0]) # x y z w y = height, z = distance glLightModelfv(GL_LIGHT_MODEL_AMBIENT, [0.2, 0.2, 0.2, 1.0]) glEnable(GL_LIGHT1) glEnable(GL_LIGHTING) glEnable(GL_DEPTH_TEST) glShadeModel(GL_SMOOTH) # most obj files expect to be smooth-shaded glEnable(GL_BLEND)#allow blending glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); #load object files after init #@todo these values should come from project? self.base = OBJ("base_bm.obj") self.shoulder = OBJ("shoulder_b.obj") self.bicep = OBJ("bicep_b.obj") self.bicep001 = OBJ("bicep001_b.obj") self.wrist = OBJ("wrist_wrail.obj") self.gripper = OBJ("gripper_b.obj") self.gripper001 = OBJ("gripper001_b.obj") self.room_T_B_B = OBJ('room_T_B.obj', swapyz=False, my_texture = True, use_mat = False) glMatrixMode(GL_PROJECTION) glLoadIdentity() if self.size: width, height = self.size else: print"no size" width, height = 800, 600 gluPerspective(45, width/float(height), 1, 2000.0) glEnable(GL_DEPTH_TEST) glMatrixMode(GL_MODELVIEW) glEnable(GL_COLOR_MATERIAL)
def InitGL(self): glLightfv(GL_LIGHT0, GL_DIFFUSE, (1, 1, 1)) glLightfv(GL_LIGHT0, GL_AMBIENT, (0.2, 0.2, 0.2)) glLightfv(GL_LIGHT0, GL_SPECULAR, (1.0, 1.0, 1.0)) glLightfv(GL_LIGHT0, GL_POSITION, (3, 3, 0.0)) glEnable(GL_LIGHT0) glEnable(GL_LIGHTING) glEnable(GL_COLOR_MATERIAL) glEnable(GL_DEPTH_TEST) glShadeModel(GL_SMOOTH) # most obj files expect to be smooth-shaded self.obj = OBJ("small_sphere.obj", swapyz=True) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(90, 800.0 / 600.0, 1, 100.0) glEnable(GL_DEPTH_TEST) glMatrixMode(GL_MODELVIEW)
def main(): homography = None # для камеры 1 camera_parameters = np.array([[693.90888662, 0, 333.27294343], [0, 688.54241995, 237.91849834], [0, 0, 1]]) # для камеры 0 #camera_parameters = np.array([[862.44357443, 0, 261.31601578], [0, 859.96288926, 176.84476175], [0, 0, 1]]) orb = cv2.ORB_create() bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) model = cv2.imread("C:/Users/tonym/Desktop/ProjectX/reference/model7.jpg", 0) kp_model = orb.detect(model) kp_model, des_model = orb.compute(model, kp_model) obj = OBJ("C:/Users/tonym/Desktop/ProjectX/models/rat.obj", swapyz=True) cap = cv2.VideoCapture(1) while cap.isOpened(): if cv2.waitKey(1) == ord('q'): break ret, scene = cap.read() if not ret: print("Unable to capture video") return kp_scene = orb.detect(scene) kp_scene, des_scene = orb.compute(scene, kp_scene) matches = bf.match(des_model, des_scene) matches = sorted(matches, key=lambda x: x.distance) if len(matches) > MIN_MATCHES: src_pts = np.float32([kp_model[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2) dst_pts = np.float32([kp_scene[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2) homography, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0) if homography is not None: try: projection = projection_matrix(camera_parameters, homography) scene = render(scene, obj, projection, model) except: pass cv2.imshow('frame', scene) cv2.imwrite("C:/Users/tonym/Desktop/ProjectX/rat_testing/img" + str(random.randint(1, 10000)) + ".jpg", scene) else: print("Недостаточно совпадений - %d/%d" % (len(matches), MIN_MATCHES)) cap.release() cv2.destroyAllWindows() return 0
def regenerate_defective_image(defective_image_path): # PYGAME pygame.init() srf = set_viewport(VIEWPORT[0], VIEWPORT[1]) # LOAD OBJECT AFTER PYGAME INIT obj = OBJ(OBJECT, swapyz=True) # clock = pygame.time.Clock() # set up OPENGL env light_position = (-40, 200, 100, 0.0) set_light_property(light_position) set_filed_of_vision(FOVY, VIEWPORT, Z_NEAR, Z_FAR) # create image part_start = time.time() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() # Defect image cartesian coordinate parameters defective_image_with_png = remove_extra_path(defective_image_path) defective_image = remove_filename_extension(defective_image_with_png) c_x, c_y, c_z, p_x, p_y, p_z, u_x, u_y, u_z = find_defect_image_target_value( defective_image) # take the shoot logging.info('Start regenerating defect image ' + defective_image) print(c_x, c_y, c_z, p_x, p_y, p_z, u_x, u_y, u_z) gluLookAt(c_x, c_y, c_z, p_x, p_y, p_z, u_x, u_y, u_z) glCallList(obj.gl_list) # Move old defect image to /home/eva/space_center/moon_8K/Regen_Image/defect_image/ defective_path = shutil.move(defective_image_path, DEFECTIVE_PATH) logging.info('{} move to {}'.format(defective_path, DEFECTIVE_PATH)) # SAVE regenerated image pygame.image.save(srf, os.path.join(PATCH_PATH, defective_image + '.png')) pygame.image.save(srf, defective_image_path) logging.info('Finish creating defect image, time = {}'.format(time.time() - part_start))
def __init__(self, fire): self.asteroid = OBJ("asteroid.obj", textureFile="asteroid.jpg") self.ellipse = Ellipse(18, 4, 0.17) self.fire = fire self.angle = 0
def load_obj(self, obj_file): self.obj = OBJ(obj_file, swapyz=True) self.Refresh(True)
up = [0, 1, 0] # for Dataset_six_random norm_forward = normalize(forward) side = normalize(crossf(norm_forward, up)) up = normalize(crossf(side, norm_forward)) return up[0], up[1], up[2] if __name__ == '__main__': EXPERIMENT_IMAGE = "near" # PYGAME pygame.init() srf = set_viewport(VIEWPORT[0], VIEWPORT[1]) # LOAD OBJECT AFTER PYGAME INIT obj = OBJ(OBJECT, swapyz=True) clock = pygame.time.Clock() # set up OPENGL env set_light_property() glEnable(GL_LIGHT0) glEnable(GL_LIGHTING) glEnable(GL_COLOR_MATERIAL) glEnable(GL_DEPTH_TEST) glShadeModel(GL_SMOOTH) # most obj files expect to be smooth-shaded set_filed_of_vision(FOVY, VIEWPORT, Z_NEAR, Z_FAR) # create image sample_target = {} part_start = time.time() logging.info('Start creating experiment image ' + EXPERIMENT_IMAGE) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity()
def transfer_pygame_surface_to_cv2_ndarray(surface): pygame_string = pygame.image.tostring(surface, 'RGB') img = np.fromstring(pygame_string, dtype=np.uint8) img = img.reshape((VIEWPORT[1], VIEWPORT[0], 3)) img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) return img if __name__ == '__main__': # PYGAME pygame.init() srf = set_viewport(VIEWPORT[0], VIEWPORT[1]) # LOAD OBJECT AFTER PYGAME INIT obj = OBJ(sys.argv[1], swapyz=True) clock = pygame.time.Clock() # set up OPENGL env set_light_property() glEnable(GL_LIGHT0) glEnable(GL_LIGHTING) glEnable(GL_COLOR_MATERIAL) glEnable(GL_DEPTH_TEST) glShadeModel(GL_SMOOTH) # most obj files expect to be smooth-shaded set_filed_of_vision(FOVY, VIEWPORT, Z_NEAR, Z_FAR) # create image for i in range(LEVEL_1_INDEX): sample_target = {} # sample_image = {} logging.info('Start creating Part_{}'.format(i)) part_start = time.time()
def main(): parser = ArgumentParser() parser.add_argument('--obj', type=str, required=True, help='OBJ filename') args = parser.parse_args() init() viewport = (800, 600) display.set_mode(viewport, OPENGL | DOUBLEBUF) display.set_caption('pyobb 3D demo') glEnable(GL_LIGHTING) glEnable(GL_LIGHT0) glLightfv(GL_LIGHT0, GL_POSITION, (0, -1, 0, 0)) glLightfv(GL_LIGHT0, GL_AMBIENT, (0.2, 0.2, 0.2, 1)) glLightfv(GL_LIGHT0, GL_DIFFUSE, (0.5, 0.5, 0.5, 1)) glEnable(GL_COLOR_MATERIAL) glEnable(GL_DEPTH_TEST) glShadeModel(GL_SMOOTH) obj = OBJ(filename=args.obj) indices = [] for face in obj.faces: indices.append(face[0][0] - 1) indices.append(face[0][1] - 1) indices.append(face[0][2] - 1) obb = OBB.build_from_triangles(obj.vertices, indices) obb_gl_list = glGenLists(1) glNewList(obb_gl_list, GL_COMPILE) glBegin(GL_LINES) glColor3fv((1, 0, 0)) def input_vertex(x, y, z): glVertex3fv(obb.transform((x, y, z))) input_vertex(*obb.max) input_vertex(obb.max[0], obb.min[1], obb.max[2]) input_vertex(obb.max[0], obb.min[1], obb.max[2]) input_vertex(obb.min[0], obb.min[1], obb.max[2]) input_vertex(obb.min[0], obb.min[1], obb.max[2]) input_vertex(obb.min[0], obb.max[1], obb.max[2]) input_vertex(obb.min[0], obb.max[1], obb.max[2]) input_vertex(*obb.max) input_vertex(obb.max[0], obb.max[1], obb.max[2]) input_vertex(obb.max[0], obb.max[1], obb.min[2]) input_vertex(obb.max[0], obb.min[1], obb.max[2]) input_vertex(obb.max[0], obb.min[1], obb.min[2]) input_vertex(obb.min[0], obb.max[1], obb.max[2]) input_vertex(obb.min[0], obb.max[1], obb.min[2]) input_vertex(obb.min[0], obb.min[1], obb.max[2]) input_vertex(obb.min[0], obb.min[1], obb.min[2]) input_vertex(obb.max[0], obb.max[1], obb.min[2]) input_vertex(obb.max[0], obb.min[1], obb.min[2]) input_vertex(obb.max[0], obb.min[1], obb.min[2]) input_vertex(*obb.min) input_vertex(*obb.min) input_vertex(obb.min[0], obb.max[1], obb.min[2]) input_vertex(obb.min[0], obb.max[1], obb.min[2]) input_vertex(obb.max[0], obb.max[1], obb.min[2]) glEnd() glEndList() clock = time.Clock() glMatrixMode(GL_PROJECTION) glLoadIdentity() width, height = viewport gluPerspective(90.0, width / float(height), 0.1, 100.0) glEnable(GL_DEPTH_TEST) glMatrixMode(GL_MODELVIEW) rotation = [0, 0] translation = [ -obb.centroid[0], -obb.centroid[1], -(obb.centroid[2] + obb.extents[2] * 2) ] rotate = move = False while True: clock.tick(30) for e in event.get(): if e.type == QUIT: exit() elif e.type == KEYDOWN and e.key == K_ESCAPE: exit() elif e.type == MOUSEBUTTONDOWN: if e.button == 4: translation[2] += 0.1 elif e.button == 5: translation[2] -= 0.1 elif e.button == 1: rotate = True elif e.button == 2: move = True elif e.type == MOUSEBUTTONUP: if e.button == 1: rotate = False elif e.button == 2: move = False elif e.type == MOUSEMOTION: i, j = e.rel if rotate: rotation[1] += i rotation[0] += j if move: translation[0] += i * .025 translation[1] -= j * .025 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() glTranslate(translation[0], translation[1], translation[2]) glRotate(rotation[0], 1, 0, 0) glRotate(rotation[1], 0, 1, 0) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) glCallList(obj.gl_list) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) glCallList(obb_gl_list) display.flip()
def step1(): fn = "obj.pkl" if 1 == 1: obj = OBJ("", "bunny.obj", swapyz=True) obj.create_bbox() with open(fn, 'wb') as f: # open file with write-mode pickle.dump(obj, f) #picklestring = pickle.dumps(summer) else: with open(fn, 'rb') as f: obj = pickle.load(f) pygame.init() viewport = (600, 600) srf = pygame.display.set_mode(viewport, OPENGL | DOUBLEBUF) light.setup_lighting() glLightfv(GL_LIGHT0, GL_POSITION, (0, 0, -100, 0.0)) glEnable(GL_DEPTH_TEST) glShadeModel(GL_SMOOTH) # most obj files expect to be smooth-shaded obj.create_gl_list() clock = pygame.time.Clock() glMatrixMode(GL_PROJECTION) glLoadIdentity() #gluPerspective(60.0, width / float(height), 1, 100.0) cam = light.camera #cam.Ortho.params=cam.Ortho.params*15 cam.Ortho.bbox[:] = cam.Ortho.bbox * 13 cam.Ortho.nf[:] = cam.Ortho.nf * 20 glOrtho(*cam.Ortho.params) glEnable(GL_DEPTH_TEST) glMatrixMode(GL_MODELVIEW) rx, ry = (0, 0) tx, ty = (0, 0) zpos = 5 rotate = move = False while 1: clock.tick(30) for e in pygame.event.get(): if e.type == QUIT: sys.exit() elif e.type == KEYDOWN and e.key == K_ESCAPE: sys.exit() elif e.type == MOUSEBUTTONDOWN: if e.button == 4: zpos = max(1, zpos - 1) elif e.button == 5: zpos += 1 elif e.button == 1: rotate = True elif e.button == 3: move = True elif e.type == MOUSEBUTTONUP: if e.button == 1: rotate = False elif e.button == 3: move = False elif e.type == MOUSEMOTION: #p(e.rel) i, j = e.rel if rotate: rx -= i ry -= j if move: tx += i ty -= j glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() # RENDER OBJECT glTranslate(tx / 20., ty / 20., -zpos) glRotate(ry / 5, 1, 0, 0) glRotate(rx / 5, 0, 0, 1) s = [10 / obj.bbox_half_r] * 3 glScale(*s) t = -obj.bbox_center glTranslate(*t) glCallList(obj.gl_list) pygame.display.flip()