#help with AI (tensorflow) , flask , python ,ESP32
31 messages · Page 1 of 1 (latest)
of course it is from chatgpt, I am bad at programming and I hope you can solve problems with this code because chtgtp itself is not that powerful
For me it is an IOT project that I have to hand in within 2 weeks.
import tensorflow as tf
import numpy as np
import cv2
import os
from moviepy.editor import VideoFileClip
import kagglehub
# Download pre-trained model
path = kagglehub.model_download("tensorflow/ssd-mobilenet-v2/tensorFlow2/fpnlite-320x320")
model = tf.saved_model.load(path)
# Labels for COCO dataset
LABELS = {1: 'person', 2: 'bicycle', 3: 'car', 4: 'motorcycle', 5: 'airplane', 6: 'bus', 7: 'train', 8: 'truck', 9: 'boat', 10: 'traffic light',
11: 'fire hydrant', 13: 'stop sign', 14: 'parking meter', 15: 'bench', 16: 'bird', 17: 'cat', 18: 'dog', 19: 'horse', 20: 'sheep',
21: 'cow', 22: 'elephant', 23: 'bear', 24: 'zebra', 25: 'giraffe', 27: 'backpack', 28: 'umbrella', 31: 'handbag', 32: 'tie',
33: 'suitcase', 34: 'frisbee', 35: 'skis', 36: 'snowboard', 37: 'sports ball', 38: 'kite', 39: 'baseball bat', 40: 'baseball glove',
41: 'skateboard', 42: 'surfboard', 43: 'tennis racket', 44: 'bottle', 46: 'wine glass', 47: 'cup', 48: 'fork', 49: 'knife', 50: 'spoon',
51: 'bowl', 52: 'banana', 53: 'apple', 54: 'sandwich', 55: 'orange', 56: 'broccoli', 57: 'carrot', 58: 'hot dog', 59: 'pizza', 60: 'donut',
61: 'cake', 62: 'chair', 63: 'couch', 64: 'potted plant', 65: 'bed', 67: 'dining table', 70: 'toilet', 72: 'tv', 73: 'laptop', 74: 'mouse',
75: 'remote', 76: 'keyboard', 77: 'cell phone', 78: 'microwave', 79: 'oven', 80: 'toaster', 81: 'sink', 82: 'refrigerator', 84: 'book',
85: 'clock', 86: 'vase', 87: 'scissors', 88: 'teddy bear', 89: 'hair drier', 90: 'toothbrush'}
input_video_path = 'C:\\Users\\User\\Desktop\\gj\\City Walking Tour 4K HDR Russian Girls _480p.mp4'
output_video_path = 'C:\\Users\\User\\Desktop\\gj\\output_video.mp4'
def detect_objects_in_frame(frame):
input_tensor = tf.convert_to_tensor(frame)
input_tensor = input_tensor[tf.newaxis, ...]
detections = model(input_tensor)
num_detections = int(detections.pop('num_detections'))
detections = {key: value[0, :num_detections].numpy() for key, value in detections.items()}
detections['num_detections'] = num_detections
boxes = detections['detection_boxes']
classes = detections['detection_classes'].astype(np.int64)
scores = detections['detection_scores']
return boxes, classes, scores
def draw_detections(frame, boxes, classes, scores, threshold=0.5):
height, width, _ = frame.shape
for i in range(len(boxes)):
if scores[i] >= threshold:
box = boxes[i]
ymin, xmin, ymax, xmax = box
xmin, xmax, ymin, ymax = int(xmin * width), int(xmax * width), int(ymin * height), int(ymax * height)
class_id = classes[i]
label = LABELS.get(class_id, 'Unknown')
if label in ['person', 'cat', 'dog']: # Change as needed
cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)
cv2.putText(frame, label, (xmin, ymin - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
return frame
def process_video(input_video_path, output_video_path):
video = VideoFileClip(input_video_path)
output_frames = []
for frame in video.iter_frames():
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
boxes, classes, scores = detect_objects_in_frame(frame_rgb)
frame_rgb = draw_detections(frame_rgb, boxes, classes, scores)
frame_bgr = cv2.cvtColor(frame_rgb, cv2.COLOR_RGB2BGR)
output_frames.append(frame_bgr)
output_video = VideoFileClip(input_video_path).set_duration(len(output_frames) / video.fps).set_fps(video.fps)
output_video = output_video.set_frames(output_frames)
output_video.write_videofile(output_video_path, codec='libx264')
process_video(input_video_path, output_video_path)
full code
problem :
from moviepy.video.io.ffmpeg_reader import ffmpeg_parse_infos
File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\moviepy\video\io\ffmpeg_reader.py", line 5, in <module>
from moviepy.editor import VideoFileClip
builtins.ImportError: cannot import name 'VideoFileClip' from partially initialized module 'moviepy.editor' (most likely due to a circular import) (C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\moviepy\editor.py)
What are you trying to build?
you mean with the code ?
Yup
I want to have a python function
which uses pre-trained tensorflow object decetion model for dictation
I want an output photo of detected things with a label on it if it is a human or an animal
I will also upload a video in which he decetes and indicate with labels with text what the deceased things are and have to upload it back to the file from where that video was
What does the input -> processing -> output look like? Do you have the image on the filesystem?
problem is that it can recoginze the video
sorry
the input is video, the process is he is going to idit the video in that idit he has to decet and put labels of the decetted people and then save video on the same path
my english is bad and also use a google translater
Makes sense
Thought Dutch people know English pretty well 
But your input video path is wrong currently -> You need to provide it with the correct path to the video
lol
but do you think that the code is good and is going to do it work?
It looks like ChatGPT stuff and you'll probably need some more modifications
Let's just approach it step by step
ok, for now i use this methode and it works
Nice