def __drawMarkerOnImage(self, mainImage, marker_id, location): config = MarkersConfiguration() color = config.color_for_marker(marker_id) if marker_id % 2 == 0: # even markers are crosses mainImage.drawCross(location, color=color) else: # odd markers are squares box = Box(location.translateBy(Vector(-9, -9)), location.translateBy(Vector(9, 9))) mainImage.drawBoxOnImage(box, color=color, thickness=4)
def _drawMarkerOnImage(self, mainImage, marker_id, location): if not str(marker_id).isdigit(): mainImage.drawCross(location, color=MarkersConfiguration.COLOR_RED) return config = MarkersConfiguration() color = config.color_for_marker(int(marker_id)) if int(marker_id) % 2 == 0: # even markers are crosses mainImage.drawCross(location, color=color) else: # odd markers are squares box = Box(location.translateBy(Vector(-9, -9)), location.translateBy(Vector(9, 9))) mainImage.drawBoxOnImage(box, color=color, thickness=4)
def test_getDriftsCount_zeroZero(self): vd = VelocityDetector() actual = vd.excludeOutliers( [Vector(0, 0), Vector(-6, 41), Vector(-6, 41), Vector(-6, 41)]) # Assert expected = [Vector(-6, 41), Vector(-6, 41), Vector(-6, 41)] self.assertEqual(Vector.vectorArrayAsString(actual), Vector.vectorArrayAsString(expected))
def test_getDriftsCount_2outliersAboveMedian(self): vd = VelocityDetector() actual = vd.excludeOutliers([ Vector(-5, 44), Vector(-5, 44), Vector(-5, 43), Vector(30, 526), Vector(30, 526) ]) # Assert expected = [Vector(-5, 44), Vector(-5, 44), Vector(-5, 43)] self.assertEqual(3, len(expected)) self.assertEqual(3, len(actual)) self.assertEqual(Vector.vectorArrayAsString(actual), Vector.vectorArrayAsString(expected))
def __driftBetweenFramesInDataFrame(self, endIndex, nextIndex): cumulativeYDrift = 0 cumulativeXDrift = 0 while nextIndex < endIndex: nextIndex = nextIndex + 1 cumulativeYDrift += self.__getYDrift(nextIndex) cumulativeXDrift += self.__getXDrift(nextIndex) return Vector(cumulativeXDrift, cumulativeYDrift)
def test_getDriftsCount_outlierIsMinimal(self): vd = VelocityDetector() actual = vd.excludeOutliers( [Vector(47, -408), Vector(-6, 41), Vector(-6, 41), Vector(-6, 41)]) # Assert expected = [Vector(-6, 41), Vector(-6, 41), Vector(-6, 41)] self.assertEqual(3, len(expected)) self.assertEqual(3, len(actual)) self.assertEqual(Vector.vectorArrayAsString(actual), Vector.vectorArrayAsString(expected))
def driftBetweenFrames(self, fromFrameID, toFrameID): # type: (int, int) -> Vector if fromFrameID < self.minFrameID() or toFrameID < self.minFrameID(): #return Vector(0,0) return None if fromFrameID > self.maxFrameID() or toFrameID > self.maxFrameID(): #return Vector(0, 0) return None if (fromFrameID > toFrameID): drift = self.driftBetweenFrames(toFrameID, fromFrameID) return Vector(-(drift.x), -(drift.y)) if (fromFrameID == toFrameID): return Vector(0, 0) return self.__getDriftBetweenTwoFrames(fromFrameID, toFrameID)
def getCrabLocation_bck(self): if self.__user_clicked_in_right_half( ) and self.__user_clicked_in_bottom_half(): # user marked crab is on "imageThis", which is the bottom right image offsetOfCrabImageFrom0x0 = Vector(-self.__boxSize, -self.__boxSize) if self.__user_clicked_in_left_half( ) and self.__user_clicked_in_bottom_half(): # user marked crab is on "imageFirst", which is bottom left image offsetOfCrabImageFrom0x0 = Vector(0, -self.__boxSize) if self.__user_clicked_in_right_half( ) and self.__user_clicked_in_top_half(): # user marked crab is on "imageLast", which is top right image offsetOfCrabImageFrom0x0 = Vector(-self.__boxSize, 0) if self.__user_clicked_in_left_half( ) and self.__user_clicked_in_top_half(): # user marked crab is on "imageMiddle", which is top left image offsetOfCrabImageFrom0x0 = Vector(0, 0)
def driftBetweenFrames(self, fromFrameID, toFrameID): # type: (int, int) -> Vector if fromFrameID < self.__driftData.minFrameID( ) or toFrameID < self.__driftData.minFrameID(): # return Vector(0,0) return None if fromFrameID > self.__driftData.maxFrameID( ) or toFrameID > self.__driftData.maxFrameID(): # return Vector(0, 0) return None if (fromFrameID == toFrameID): return Vector(0, 0) driftX = self.getXDriftPixels(fromFrameID, toFrameID) driftY = self.getYDriftPixels(fromFrameID, toFrameID) return Vector(driftX, driftY)
def __determineLocationOfTextBox(self, location): boxHeight = 25 boxWidth = 50 if location.y < boxHeight: translateYBy = 5 else: translateYBy = (boxHeight + 5) * (-1) if location.x < boxWidth: translateXBy = 5 else: translateXBy = (boxWidth + 5) * (-1) topLeftOfTextBox = location.translateBy( Vector(translateXBy, translateYBy)) bottomRightOfTextBox = topLeftOfTextBox.translateBy( Vector(boxWidth, boxHeight)) textBox = Box(topLeftOfTextBox, bottomRightOfTextBox) return textBox
def __driftToNearbyFrame(self, index, nearbyFrameID): framesToJump = int(self.__getFrameID(index)) - nearbyFrameID yPixelsDriftPerFrame = self.__pixelsYDriftPerFrame(index) yPixelsAway = math.floor(yPixelsDriftPerFrame * framesToJump) xPixelsDriftPerFrame = self.__pixelsXDriftPerFrame(index) xPixelsAway = math.floor(xPixelsDriftPerFrame * framesToJump) return Vector(xPixelsAway, yPixelsAway)
def getCrabLocation(self): if self.__user_clicked_in_right_half(): xOffset = -self.__boxSize else: xOffset = 0 if self.__user_clicked_in_bottom_half(): yOffset = -self.__boxSize else: yOffset = 0 offsetOfCrabImageFrom0x0 = Vector(xOffset, yOffset) return self.__crabCoordinatesOnItsFrame(self.getFrameIDOfCrab(), offsetOfCrabImageFrom0x0)
def getImgObj(self): # type: () -> Image imgObj = self.frameDeco.getImgObj().copy() frame_id = self.frameDeco.getFrameID() distancePix = int(self.__redDotsData.getDistancePixels(frame_id)) #draw 8 grid squares along X and Y axis. for i in range(-4, 4): #draw 9 X-lines. One through midpoint between red dots (i=0) and then 4 north-west and 4 south-east point = self.__startPoint.translateBy( Vector(i * distancePix, i * distancePix)) self.__drawGridX(imgObj, point, 0) return imgObj
def ttest_getDriftsCount_outlierIsSymmetricalOverZero(self): vd = VelocityDetector() actual = vd.excludeOutliers( [Vector(0, 30), Vector(0, 30), Vector(0, -32)]) # Assert expected = [Vector(0, 30), Vector(0, 30)] self.assertEqual(2, len(expected)) self.assertEqual(2, len(actual)) self.assertEqual(Vector.vectorArrayAsString(actual), Vector.vectorArrayAsString(expected))
def test_getDriftsCount_twoValuesButVeryDifferent(self): vd = VelocityDetector() actual = vd.excludeOutliers([Vector(47, -408), Vector(-6, 41)]) # Assert expected = [Vector(47, -408), Vector(-6, 41)] self.assertEqual(2, len(expected)) self.assertEqual(2, len(actual)) self.assertEqual(Vector.vectorArrayAsString(actual), Vector.vectorArrayAsString(expected))
def __getDriftBetweenTwoFrames(self, fromFrameID, toFrameID): #assuming fromFrameID is less than or equal to toFrameID and both are within valid range startingFrameIDInDataFrame = self.__nextFrameIDInFile(fromFrameID) startIndex = self.__getIndexOfFrame(startingFrameIDInDataFrame) endingFrameIDInDataFrame = self.__nextFrameIDInFile(toFrameID) endIndex = self.__getIndexOfFrame(endingFrameIDInDataFrame) driftToStartingFrame = self.__driftToNearbyFrame( startIndex, fromFrameID) cumulativeDrift = self.__driftBetweenFramesInDataFrame( endIndex, startIndex) driftFromEndingFrame = self.__driftToNearbyFrame(endIndex, toFrameID) totalYDrift = cumulativeDrift.y + driftToStartingFrame.y - driftFromEndingFrame.y totalXDrift = cumulativeDrift.x + driftToStartingFrame.x - driftFromEndingFrame.x return Vector(totalXDrift, totalYDrift)