Appearance
question:Implement a Stack using an array and Linked lists in Python. #Stack Implementation using Lists class Stack: def __init__(self): self.stack = [] def push(self, value): # Add an element at the end of the stack self.stack.append(value) def pop(self): # Remove an element from the end of the stack if len(self.stack) < 1: return None return self.stack.pop() def size(self): # Get the size of the stack return len(self.stack) def is_empty(self): # Check if the stack is empty return len(self.stack) == 0 def peek(self): # Return the top element of the stack if len(self.stack) < 1: return None return self.stack[-1] class StackLL: class Node: def __init__(self, value): self.value = value self.next = None def __init__(self): self.head = None def push(self, value): # Add a Node with value at the head of the list newnode = StackLL.Node(value) newnode.next = self.head self.head = newnode def pop(self): # Remove a Node from the head of the list if not self.head: return None popped_node = self.head self.head = self.head.next popped_node.next = None return popped_node.value def size(self): # Get the size of the list size = 0 node = self.head while node: size += 1 node = node.next return size def is_empty(self): # Check if the list is empty return self.size() == 0 def peek(self): if self.head is None: return None # or the data of the head node return self.head.value stack_list = Stack() stack_ll = StackLL() while True: print('1. Stack Using List') print('2. Stack Using Linked List') print('3. Quit') choice = input('Enter your choice: ') if choice == '3': break stack_type = input(1. Push, 2. Pop, 3. Check empty, 4. Size, 5. Peek ) if choice == '1': if stack_type == 1: value = input(Enter a value: ) stack_list.push(value) elif stack_type == 2: print(Popped Value:, stack_list.pop()) elif stack_type == 3: print(Is Stack Empty?
answer:class StackArray: def __init__(self): self.stack = [] def push(self, value): # Add an element to the top of the stack self.stack.append(value) def pop(self): # Remove the top element of the stack if len(self.stack) == 0: return None return self.stack.pop() def size(self): # Get the size of the stack return len(self.stack) def is_empty(self): # Check if the stack is empty return len(self.stack) == 0 def peek(self): # Return the top element of the stack without removing it if len(self.stack) == 0: return None return self.stack[-1] class StackLinkedList: class Node: def __init__(self, value): self.value = value self.next = None def __init__(self): self.head = None def push(self, value): # Add a new node with value at the head of the linked list new_node = self.Node(value) new_node.next = self.head self.head = new_node def pop(self): # Remove the head node if not self.head: return None popped_value = self.head.value self.head = self.head.next return popped_value def size(self): # Get the size of the linked list (number of nodes) count = 0 current = self.head while current: count += 1 current = current.next return count def is_empty(self): # Check if the linked list is empty return self.head is None def peek(self): # Return the head node value if self.head is None: return None return self.head.value
question:def greet(name): This function greets a person print("Hello " + name) greet("John")
answer:def greet(name): This function greets a person by their name. return "Hello " + name
question:def most_common_elements(nums): count = {} for num in nums: count[num] = count.get(num, 0) + 1 max_count = max(count.values()) return [num for num in count if count[num] == max_count]
answer:def most_common_elements(nums): Returns a list of the most common elements in the list nums. If there are multiple elements with the highest frequency, all are included in the list. :param nums: List[int] - A list of integers. :return: List[int] - A list of the most common elements. count = {} for num in nums: count[num] = count.get(num, 0) + 1 max_count = max(count.values(), default=0) # default=0 to handle empty list case return [num for num in count if count[num] == max_count]
question:import os import cv2 import numpy as np from datetime import datetime def draw_Bounding_Rectangle(dir_images, video_writer, bbs_list, frame_counter): show = False if show: # Decode the frame image_path = os.path.join(dir_images, 'frame%d.jpg' % frame_counter) image = cv2.imread(image_path) for bb in bbs_list: # Map DDD (degree, degree, distance) to xyx'y' x = int(bb[0] * image.shape[1] / 100) y = int(bb[1] * image.shape[0] / 100) w = int(bb[2] * image.shape[1] / 100) h = int(bb[3] * image.shape[0] / 100) # Draw rectangles cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2) cv2.putText(image, str(bb[4]), (x, y-10), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.5, color=(0, 255, 0), thickness=2) # write it to cv2 videoWriter video_writer.write(np.copy(image)) cv2.imshow('display',image) if cv2.waitKey(1) & 0xFF == ord('q'): return 'Stop' def main(): if not os.path.exists('./outputs/output.avi'): os.mkdir('inputs/boundingboxesfile.txt') dir_images = '..NN1 southeast images' dir_output_video = '..NN1 southeastoutputs' try: video_writer = cv2.VideoWriter('output1.mp4', cv2.VideoWriter_fourcc(*'mp4v'), 25.0, (1920, 1080)) if not dir_images: print('bad dir : %s' % dir_images) else: # Read images num_files = len(os.listdir(dir_images)) bbs_file = open('_inputs/boundingboxesfile.txt','r') frame_counter = 224 while True: line = bbs_file.readline() if not line: break data = line.split(',') bbs_list = list( list(map(float, data[1:5])) + [data[0]]) return_val = draw_Bounding_Rectangle(dir_images, video_writer, bbs_list, frame_counter) frame_counter += 1 if return_val == 'Stop': break cv2.destroyAllWindows() except Exception as e: print (e) if __name__== "__main__": main() ``` I am trying to read an initial frame from my image directory and only start video generation when bounding box information is located after a certain frame number. Let's say frame 224. Therefore I will only start to read bounding box information after reading the line where the frame number is 224 in the bounding box file. I tried modifying your code to the following: ``` def main(): dir_images = '..NN1 southeast images' dir_output_video = '..NN1 southeastoutputs' try: video_writer = cv2.VideoWriter(os.path.join(dir_output_video, 'output.avi'), cv2.VideoWriter_fourcc(*'XVID'), 25.0, (1920, 1080)) if not dir_images: print('bad dir : %s' % dir_images) else: num_files = len(os.listdir(dir_images)) bbs_file = open('inputs/boundingboxesfile.txt','r') frame_counter = 0 in_sequence = False while True: line = bbs_file.readline() if not line: break if not in_sequence: data = line.split(',') frame_number = int(data[0]) if frame_number >= 224: in_sequence = True else: continue data = line.split(',') bbs_list = list( list(map(float, data[1:5])) + [data[0]]) return_val = draw_Bounding_Rectangle(dir_images, video_writer, bbs_list, frame_counter) frame_counter += 1 except Exception as e: print (e) ``` This works but I feel like it reads and splits the same line twice. I can modify it to this, does it correctly skip the frames before `frame_number` 224 and draw only those after?
answer:import os import cv2 import numpy as np def draw_Bounding_Rectangle(dir_images, video_writer, bbs_list, frame_counter): show = False if show: # Decode the frame image_path = os.path.join(dir_images, 'frame%d.jpg' % frame_counter) image = cv2.imread(image_path) for bb in bbs_list: # Map DDD (degree, degree, distance) to xyx'y' x = int(bb[0] * image.shape[1] / 100) y = int(bb[1] * image.shape[0] / 100) w = int(bb[2] * image.shape[1] / 100) h = int(bb[3] * image.shape[0] / 100) # Draw rectangles cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2) cv2.putText(image, str(bb[4]), (x, y-10), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.5, color=(0, 255, 0), thickness=2) # write it to cv2 videoWriter video_writer.write(np.copy(image)) cv2.imshow('display',image) if cv2.waitKey(1) & 0xFF == ord('q'): return 'Stop' def main(): dir_images = '..NN1 southeast images' dir_output_video = '..NN1 southeastoutputs' try: os.makedirs(dir_output_video, exist_ok=True) video_writer = cv2.VideoWriter(os.path.join(dir_output_video, 'output.avi'), cv2.VideoWriter_fourcc(*'XVID'), 25.0, (1920, 1080)) bbs_file_path = 'inputs/boundingboxesfile.txt' if not os.path.exists(bbs_file_path): print("Bounding box file not found.") return bbs_file = open(bbs_file_path,'r') frame_counter = 224 in_sequence = False while True: line = bbs_file.readline() if not line: break data = line.strip().split(',') frame_number = int(data[0]) if frame_number >= 224: bbs_list = list(map(float, data[1:5])) + [data[0]] return_val = draw_Bounding_Rectangle(dir_images, video_writer, bbs_list, frame_counter) frame_counter += 1 if return_val == 'Stop': break bbs_file.close() cv2.destroyAllWindows() except Exception as e: print(e)