def update(self): self.historyFrameCounter += 1 self.episodeFrameCounter += 1 if self.episodeFrameCounter % 1000 == 0: print 'frame #' + str(self.episodeFrameCounter) if self.velocity > 0: self.velocity = max(0, self.velocity - FRICTION) elif self.velocity < 0: self.velocity = min(0, self.velocity + FRICTION) self.velocity = self.velocity + self.accSignal * ACCELERATION_COEFF self.velocity = clamp(self.velocity, MIN_VELOCITY, MAX_VELOCITY) self.turnSignal = clamp(self.turnSignal, -1, 1) theta = -1.0 * self.turnSignal / 360 * 2 * pi # Clear signal self.accSignal = 0 self.turnSignal = 0 fx = self.faceVec.getX() fy = self.faceVec.getY() self.faceVec = Vec2(fx * cos(theta) - fy * sin(theta), fx * sin(theta) + fy * cos(theta)) self.faceVec.normalize() speedVec = self.faceVec * self.velocity self.camPos += Vec3(speedVec.getX(), speedVec.getY(), 0) deg = Vec2(0, 1).signedAngleDeg(self.faceVec) base.cam.setHpr(deg, PITCH, 0) base.cam.setPos(self.camPos) self.npBox.setPos(self.camPos) self.npBox.setHpr(deg, 0, 0) screenshot = PNMImage() if not self.drCar.getScreenshot(screenshot): return screenshot.removeAlpha() reward = self.calculateReward(screenshot) if self.camPos.getY() < -9.5 or self.camPos.getY() > 9.5 \ or self.camPos.getX() < -9.5 or self.camPos.getX() > 9.5 \ or self.episodeFrameCounter > EPISODE_FRAME_LIMIT \ or self.deviationCounter > DEVIATION_LIMIT: self.reset() self.feedback(screenshot, reward, 1 if self.episodeFrameCounter == 0 else 0) return
def saveImage(self, path='testImg.png'): self.base.graphicsEngine.renderFrame() # Rendering the frame self.base.graphicsEngine.renderFrame() # Rendering the frame image = PNMImage() dr = self.base.camNode.getDisplayRegion(0) dr.getScreenshot(image) image.removeAlpha() image.write(Filename(path)) # pre process for skeltonization gray = cv2.imread(path, cv2.IMREAD_GRAYSCALE) gray[gray < 150] = 0 gray[gray >= 150] = 255 kernel = np.ones((3, 3), np.uint8) gray = cv2.erode(gray, kernel) #gray = cv2.dilate(gray, kernel) #gray = cv2.resize(gray, (gray.shape[1]/2, gray.shape[0]/2)) cv2.imwrite(path, gray) '''
def _write_bitmap(self, fp, image, size, bpp): """ Writes the bitmap header and data of an .ico file. """ fp.write( struct.pack('<IiiHHIIiiII', 40, size, size * 2, 1, bpp, 0, 0, 0, 0, 0, 0)) # XOR mask if bpp == 24: # Align rows to 4-byte boundary rowalign = b'\0' * (-(size * 3) & 3) for y in xrange(size): for x in xrange(size): r, g, b = image.getXel(x, size - y - 1) fp.write( struct.pack('<BBB', int(b * 255), int(g * 255), int(r * 255))) fp.write(rowalign) elif bpp == 32: for y in xrange(size): for x in xrange(size): r, g, b, a = image.getXelA(x, size - y - 1) fp.write( struct.pack('<BBBB', int(b * 255), int(g * 255), int(r * 255), int(a * 255))) elif bpp == 8: # We'll have to generate a palette of 256 colors. hist = PNMImage.Histogram() image2 = PNMImage(image) if image2.hasAlpha(): image2.premultiplyAlpha() image2.removeAlpha() image2.quantize(256) image2.make_histogram(hist) colors = list(hist.get_pixels()) assert len(colors) <= 256 # Write the palette. i = 0 while i < 256 and i < len(colors): r, g, b, a = colors[i] fp.write(struct.pack('<BBBB', b, g, r, 0)) i += 1 if i < 256: # Fill the rest with zeroes. fp.write(b'\x00' * (4 * (256 - i))) # Write indices. Align rows to 4-byte boundary. rowalign = b'\0' * (-size & 3) for y in xrange(size): for x in xrange(size): pixel = image2.get_pixel(x, size - y - 1) index = colors.index(pixel) if index >= 256: # Find closest pixel instead. index = closest_indices[index - 256] fp.write(struct.pack('<B', index)) fp.write(rowalign) else: raise ValueError("Invalid bpp %d" % (bpp)) # Create an AND mask, aligned to 4-byte boundary if image.hasAlpha() and bpp <= 8: rowalign = b'\0' * (-((size + 7) >> 3) & 3) for y in xrange(size): mask = 0 num_bits = 7 for x in xrange(size): a = image.get_alpha_val(x, size - y - 1) if a <= 1: mask |= (1 << num_bits) num_bits -= 1 if num_bits < 0: fp.write(struct.pack('<B', mask)) mask = 0 num_bits = 7 if num_bits < 7: fp.write(struct.pack('<B', mask)) fp.write(rowalign) else: andsize = (size + 7) >> 3 if andsize % 4 != 0: andsize += 4 - (andsize % 4) fp.write(b'\x00' * (andsize * size))