def timestamp_filter(video_frame, t): printtime = make_datetime_string(folder[i]) + datetime.timedelta( seconds=t) #t = time of current frame im = Image.fromarray(video_frame(t)) draw = ImageDraw.Draw(im) draw.text((2, 2), str(printtime), font=font, fill='white') return PIL_to_npimage(im)
def timecode_frame(time): """ Custom make frame for timecode. """ timecode_image = Image.new('RGB', (200, 50)) draw = ImageDraw.Draw(timecode_image) draw.text((50, 25), "%.02f" % (time)) return PIL_to_npimage(timecode_image)
def timecode_frame(time): """ Custom make frame for timecode. """ timecode_image = Image.new('RGB', (100, 40)) draw = ImageDraw.Draw(timecode_image) draw.text((10, 10), "%.02f" % time) return PIL_to_npimage(timecode_image)
def get_static_text(shape): height, width, _ = shape im = PIL.Image.new('RGB', (width, height)) draw = ImageDraw.Draw(im) draw.text((width*35/1000, height*4/100), "throttle", (255, 0, 0), font=SMALL_FONT) draw.text((width*35/1000, height*94/100), "brake", (255, 0, 0), font=SMALL_FONT) return PIL_to_npimage(im)
def add_timestamp(get_frame, t): """ This function returns a 'region' of the current frame. The position of this region depends on the time. """ frame = get_frame(t) starttime = datetime.strptime(rawstring[1], "%Y-%m-%d_%H%M%S") ct = starttime + timedelta(0, t) ct_string = ct.strftime('%Y-%m-%d %H:%M:%S') im = Image.fromarray(frame) draw = ImageDraw.Draw(im) draw.text((50, 650), str(ct_string), font=font, fill="yellow") return PIL_to_npimage(im)
def overlay_image(frame, image, y, x): np_image = PIL_to_npimage(image) height, width, _ = np_image.shape mask = np.sum(np_image, axis=-1) new_img = np.empty(np_image.shape) new_img[:, :, 0] = mask new_img[:, :, 1] = mask new_img[:, :, 2] = mask maximum = np.max(new_img) if maximum == 0: return new_img /= maximum patch = frame[y:y+height, x:x+width, :] patch[:, :, :] = (1 - new_img)*patch + new_img*np_image
def prepare_throttle_bar(frame): frame_height, frame_width, _ = frame.shape bar_height = int(frame_height*THROTTLE_BAR_HEIGHT) bar_width = int(frame_width*THROTTLE_BAR_WIDTH) bar_center = int(bar_height*THROTTLE_BAR_CENTER) shape_im = PIL.Image.new('RGB', (bar_width, bar_height)) draw = ImageDraw.Draw(shape_im) top_gradient = np.tile(np.linspace(1, 0, bar_center), (bar_width, 1)).T bottom_gradient = np.tile(np.linspace(0, 1, bar_height - bar_center), (bar_width, 1)).T draw.polygon([ (0, 0), (bar_width, 0), (bar_width/2, bar_center), (bar_width, bar_height), (0, bar_height) ], fill=(1, 1, 1)) mask = PIL_to_npimage(shape_im) bar = np.empty((bar_height, bar_width, 3), dtype='uint8') top_red, top_green = red_green_gradient(top_gradient) bottom_red, bottom_green = red_green_gradient(bottom_gradient) bar[0:bar_center, :, 0] = top_red*255 bar[0:bar_center, :, 1] = top_green*255 bar[0:bar_center, :, 2] = 0 bar[bar_center:bar_height, :, 0] = bottom_red*255 bar[bar_center:bar_height, :, 1] = bottom_green*255 bar[bar_center:bar_height, :, 2] = 0 bar *= mask return bar
def make_frame(self, t): note = 'G#5' # Setup landscape im = plim.new('RGB', size) draw = ImageDraw.Draw(im) if self.test == False: note_type = "quarter" # Saves Sharp/Flat for later if '#' in note: self.modifier += 1 elif 'b' in note: self.modifier -= 1 note = note.replace('#', '') try: staff_position = staff_notes_high.index(note) y = 265 if int(note[1:]) >= 5: if note_type != 'whole': self.add_lower_line(y + (staff_position * 1.8), t) if staff_position <= 8: for x in range(8, staff_position - 1, -2): self.add_underline(y + (x * 1.8), t) else: if note_type != 'whole': self.add_upper_line(y + (staff_position * 1.8), t) except Exception as e: staff_position = staff_notes_low.index(note) y = 310 if int(note[1:]) == 3: if note_type != 'whole': self.add_lower_line(y + (staff_position * 1.8), t) else: if note_type != 'whole': self.add_upper_line(y + (staff_position * 1.8), t) if staff_position >= 11: for x in range(11, staff_position + 1, 2): self.add_underline(y + (x * 1.8), t) # Add note and timing to the spooler for sheet music self.add_circle(note_type, y + (staff_position * 1.8), t, self.modifier) # Add sharp icon if self.modifier == 1: self.add_sharp(y + (staff_position * 1.8), t) self.test = True # Check to see if a symbol (circle with a line) for sheet music nees to be popped as well if len(self.spool_circle_timer) > 0: # If the circle is done being shown - pop it from the spooler if (t - float(self.spool_circle_timer[0])) > float( instrument_info[1][0]): self.spool_circle.pop(0) self.spool_circle_timer.pop(0) if len(self.spool_line_timer) > 0: # If the line is done being shown - pop it from the spooler if (t - float(self.spool_line_timer[0])) > float( instrument_info[1][0]): self.spool_line.pop(0) self.spool_line_timer.pop(0) # Draw the circles from spool if not empty for x in range(0, len(self.spool_circle)): exec(self.spool_circle[x]) # Setup for drawing lines drawl = aggdraw.Draw(im) drawl.setantialias(False) p = aggdraw.Pen(color=black, width=1) # Draw the lines from spool if not empty for x in range(0, len(self.spool_line)): exec(self.spool_line[x]) # Whoosh! drawl.flush() # Returns the instance frame to be added to the movie return PIL_to_npimage(im)
def addText(matrix, text): im = Image.fromarray(np.uint8(matrix)) draw = ImageDraw.Draw(im) draw.text((50, 25), str(text)) return PIL_to_npimage(im)
def to_frame(self): return PIL_to_npimage(self._make_material().convert('RGBA'))
def make_frame(self, t): # Setup landscape im = plim.new('RGB', size) draw = ImageDraw.Draw(im) # If the next note time is up - add it to the spool if (self.frame < len(pick[0])) and (t > float( str(pick[1][self.frame])[:5])): # Reset vars. pos = [] brk = False # If the note's magnitude is above the threshold, and standard deviation for salience, show the note if (int(pick[2][self.frame]) >= self.threshold) and (float( pick[5][self.frame]) > std): # Convert note into string, position, sharpness & how it appears in video try: # Gets the current note from pickle and format note = str(pick[0][self.frame][0]).replace("'", "") note = note[:len(note) - 1] + '-' + note[len(note) - 1:] note_len = len(note) note_str = note note = Note(note) loop_counter = 0 # Enables note switching of octaves while True: if (len(instrument.find_fingering( [note])) == 0) or (instrument.find_fingering( [note])[0][0][1] > int(instrument_info[4][0])): # Finger position is unavailable if len(instrument.find_fingering([note])) == 0: if int(note_str[note_len - 1:]) > int( instrument_info[4][0]): note.octave_down() else: note.octave_up() # Finger position is greater than max elif instrument.find_fingering( [note])[0][0][1] > int(instrument_info[4][0]): note.octave_down() else: break if loop_counter == 6: brk = True break loop_counter += 1 # Something something something note = str(note).replace("'", "") string, pos_note = instrument.find_fingering([note])[0][0] position.append(string) # Color of animation depending on pitch grade if pos_note == (int(instrument_info[4][0]) + 1): pos_note -= 1 color = higher elif ('#' in note) and (pos_note != 0): color = higher elif ('#' in note) and (pos_note == 0): pos_note += 1 color = lower elif 'b' in note: color = lower else: color = norm # Get the position of note for animation pos = instrument_info[0][string] # Error with converting note - omit it from spooler except Exception as e: position.append('10') brk = True else: position.append('10') brk = True # Found a note to use if brk == False: # Add note and timing to the spooler self.add_note(pos, pos_note, t, color) # Saves Sharp/Flat for later if '#' in note: self.modifier += 1 elif 'b' in note: self.modifier -= 1 # Add the generated sheet music try: determine = self.getProximity() note = note.replace('-', '').replace('#', '') if determine == 0.25: note_type = 'quarter' elif determine == 0.5: note_type = 'half' elif determine == 0.9: note_type = 'whole' except Exception as e: note_type = 'quarter' pass note = (note.replace('#', '')).replace('-', '') try: staff_position = staff_notes_high.index(note) y = 265 if int(note[1:]) >= 5: if note_type != 'whole': self.add_lower_line(y + (staff_position * 1.8), t) if staff_position <= 8: for x in range(8, staff_position - 1, -2): self.add_underline(y + (x * 1.8), t) else: if note_type != 'whole': self.add_upper_line(y + (staff_position * 1.8), t) except Exception as e: staff_position = staff_notes_low.index(note) y = 310 if int(note[1:]) == 3: if note_type != 'whole': self.add_lower_line(y + (staff_position * 1.8), t) else: if note_type != 'whole': self.add_upper_line(y + (staff_position * 1.8), t) if staff_position >= 11: for x in range(11, staff_position + 1, 2): self.add_underline(y + (x * 1.8), t) # Add note and timing to the spooler for sheet music self.add_circle(note_type, y + (staff_position * 1.8), t, self.modifier) # Add sharp icon if present in note if self.modifier == 1: self.add_sharp(y + (staff_position * 1.8), t) # If the spooler is empty - get it ready if len(self.spool) == 0: self.last_pop_frame = self.frame self.last_pop_frame_time = str(pick[1][self.frame])[:5] # Increment to the next note self.frame += 1 # Check to see if a note needs to be popped from spool: if len(self.spool_note_timer) > 0: # If the note is done being played - pop it from the spooler if (t - float(self.spool_note_timer[0])) > float( instrument_info[1][0]): self.spool.pop(0) self.spool_note_timer.pop(0) self.last_pop_frame += 1 if (self.frame < len(pick[0])): self.last_pop_frame_time = str( pick[1][self.last_pop_frame])[:5] # Check to see if a symbol (circle with a line) for sheet music nees to be popped as well if len(self.spool_circle_timer) > 0: # If the circle is done being shown - pop it from the spooler if (t - float(self.spool_circle_timer[0])) > float( instrument_info[1][0]): self.spool_circle.pop(0) self.spool_circle_timer.pop(0) # Lines pop if len(self.spool_line_timer) > 0: # If the line is done being shown - pop it from the spooler while True: try: if (t - float(self.spool_line_timer[0])) > float( instrument_info[1][0]): self.spool_line.pop(0) self.spool_line_timer.pop(0) else: break except Exception as e: break # Sharp symbol pop if len(self.spool_sharp_timer) > 0: while True: try: # If the line is done being shown - pop it from the spooler if (t - float(self.spool_sharp_timer[0])) > float( instrument_info[1][0]): for x in range(0, 4): self.spool_sharp.pop(x) self.spool_sharp_timer.pop(x) else: break except Exception as e: break # Draw the notes from spool if not empty for x in range(0, len(self.spool)): exec(self.spool[x]) # Draw the circles from spool if not empty for x in range(0, len(self.spool_circle)): exec(self.spool_circle[x]) # Setup for drawing lines drawl = aggdraw.Draw(im) drawl.setantialias(False) p = aggdraw.Pen(color=black, width=2) p_thin = aggdraw.Pen(color=black, width=1) # Draw the lines from spool if not empty for x in range(0, len(self.spool_line)): exec(self.spool_line[x]) # Draw the sharps from spool if not empty for x in range(0, len(self.spool_sharp)): exec(self.spool_sharp[x]) # Whoosh! drawl.flush() # Returns the instance frame to be added to the movie return PIL_to_npimage(im)
def make_frame(self,t): # Setup landscape im = plim.new('RGB',size) draw = ImageDraw.Draw(im) # If the next note time is up - add it to the spool if (self.frame < len(pick[0])) and (t > float(str(pick[1][self.frame])[:5])): # Reset vars. pos = [] brk = False # If the note's magnitude is above the threshold, show the note if int(pick[2][self.frame]) >= self.threshold: # Convert note into string, position, sharpness & how it appears in video try: # If the note is within frequency range #if int(instrument_info[3][1][0]) > float(librosa.note_to_hz(pick[0][self.frame][0])[0]) > int(instrument_info[3][0][0]): # Gets the current note from pickle and format note = str(pick[0][self.frame][0]).replace("'","") note = note[:len(note)-1] + '-' + note[len(note)-1:] note_len = len(note) note_str = note note = Note(note) loop_counter = 0 while True: if (len(instrument.find_fingering([note])) == 0) or (instrument.find_fingering([note])[0][0][1] > int(instrument_info[4][0])): # Finger position is unavailable if len(instrument.find_fingering([note])) == 0: if int(note_str[note_len-1:]) > int(instrument_info[4][0]): note.octave_down() else: note.octave_up() # Finger position is greater than max elif instrument.find_fingering([note])[0][0][1] > int(instrument_info[4][0]): note.octave_down() else: break if loop_counter == 6: brk = True break loop_counter += 1 if brk == False: note = str(note).replace("'","") string, pos_note = instrument.find_fingering([note])[0][0] position.append(string) # Color of animation depending on pitch grade if pos_note == (int(instrument_info[4][0])+1): pos_note -= 1 color = higher elif ('#' in note) and (pos_note != 0): color = higher elif ('#' in note) and (pos_note == 0): pos_note += 1 color = lower elif 'b' in note: color = lower else: color = norm # Get the position of note for animation pos = instrument_info[0][string] else: position.append('10') #else: # position.append('10') # brk = True # Error with converting note - omit it from spooler except Exception as e: position.append('10') brk = True else: position.append('10') brk = True # Found a note to use if brk == False: # Add note and timing to the spooler self.add(pos,pos_note,t,color) # If the spooler is empty - get it ready if len(self.spool) == 0: self.last_pop_frame = self.frame self.last_pop_frame_time = str(pick[1][self.frame])[:5] # Increment to the next note self.frame += 1 # Check to see if a note needs to be popped from spool: if len(self.spool_timer) > 0: # If the note is done being played - pop it from the spooler if (t - float(self.spool_timer[0])) > float(instrument_info[1][0]): self.spool.pop(0) self.spool_timer.pop(0) self.last_pop_frame += 1 self.last_pop_frame_time = str(pick[1][self.last_pop_frame])[:5] # Draw the lines from spool if not empty for x in range(0,len(self.spool)): exec(self.spool[x]) # Returns the instance frame to be added to the movie return PIL_to_npimage(im)