NKASG commited on
Commit
81d9d84
·
verified ·
1 Parent(s): cd07294

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -29
app.py CHANGED
@@ -1,29 +1,54 @@
1
- import tensorflow as tf
2
- from tensorflow import keras
3
- from tensorflow.keras.preprocessing import image_dataset_from_directory
4
- from tensorflow.keras import models, layers
5
- import numpy as np
6
- import pandas as pd
7
- import matplotlib.pyplot as plt
8
- import gradio as gr
9
- from transformers import AutoTokenizer, AutoModelForImageClassification
10
- from PIL import Image
11
- import requests
12
- import torch
13
-
14
- # Load model from Hugging Face model hub
15
- model_name = "NKASG/GNN" # Replace with your model's name on Hugging Face
16
- tokenizer = AutoTokenizer.from_pretrained(model_name)
17
- model = AutoModelForImageClassification.from_pretrained(model_name)
18
-
19
- # Define function for image preprocessing and prediction
20
- def process_image(image):
21
-
22
- img_4d=img.reshape(-1,256,256,3)
23
- prediction=model.predict(img_4d)[0]
24
- # return {class_names[i]: float(prediction[i]) for i in range(3)}
25
-
26
- image = gr.inputs.Image(shape=(256,256))
27
- label = gr.outputs.Label(num_top_classes=1)
28
-
29
- gr.Interface(fn=predict_image, inputs=image, outputs=label,interpretation='default').launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from ultralytics import YOLO
2
+ import cv2
3
+ import cvzone
4
+ import math
5
+ import imutils
6
+ from imutils.video import VideoStream
7
+
8
+ cap = cv2.VideoCapture(0)#'rtsp://admin:[email protected]:554/Streaming/Channels/1')
9
+ cap.set(3, 640)
10
+ cap.set(4, 128)
11
+
12
+ #cap = cv2.VideoCapture("C:/Users/Moh/PycharmProjects/pythonProject/video/GVD.mp4")
13
+
14
+ model = YOLO("best20.pt")
15
+
16
+ classNames = ['person', 'Gun' ]
17
+
18
+ while True:
19
+ success, frame = cap.read()
20
+
21
+ result = model(frame, stream=True)
22
+
23
+ for r in result:
24
+ boxes = r.boxes
25
+ for box in boxes:
26
+ # bounding box
27
+ x1, y1, x2, y2 = box.xyxy[0]
28
+ x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
29
+ w, h = x2 - x1, y2 - y1
30
+
31
+ #confidence
32
+
33
+ conf = math.ceil((box.conf[0] * 100)) / 100
34
+
35
+ #className
36
+
37
+ cls = int(box.cls[0])
38
+ currentClass = classNames[cls]
39
+
40
+ if currentClass == "Gun" and conf > 0.4:
41
+ print(conf)
42
+
43
+ cvzone.cornerRect(frame, (x1, y1, w, h), l=9, rt=5, colorR=(255, 0, 0))
44
+ cvzone.putTextRect(frame, f'{conf}{currentClass}', (max(0, x1), max(0, y1)))
45
+ print(conf)
46
+
47
+ frame = imutils.resize(frame, width=1200)
48
+ cv2.imshow('AsimCodeCam', frame)
49
+ key = cv2.waitKey(1) & 0xFF
50
+ if key == ord('q'):
51
+ break
52
+
53
+ cv2.destroyAllWindows()
54
+ video_stream.stop()