Spaces:
Configuration error
Configuration error
File size: 21,568 Bytes
26187fd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 |
import cv2
import datetime
import imutils
import numpy as np
from centroidtracker import CentroidTracker
import pandas as pd
import torch
import streamlit as st
import mediapipe as mp
import cv2 as cv
import numpy as np
import tempfile
import time
from PIL import Image
import pandas as pd
import torch
import base64
import streamlit.components.v1 as components
import csv
import pickle
from pathlib import Path
import streamlit_authenticator as stauth
import os
import csv
# x-x-x-x-x-x-x-x-x-x-x-x-x-x LOGIN FORM x-x-x-x-x-x-x-x-x
import streamlit as st
import pandas as pd
import hashlib
import sqlite3
#
import pickle
from pathlib import Path
import streamlit_authenticator as stauth
# print("Done !!!")
data = ["student Count",'Date','Id','Mobile','Watch']
with open('final.csv', 'w') as file:
writer = csv.writer(file)
writer.writerow(data)
l1 = []
l2 = []
if st.button('signup'):
usernames = st.text_input('Username')
pwd = st.text_input('Password')
l1.append(usernames)
l2.append(pwd)
names = ["dmin", "ser"]
if st.button("signupsss"):
username =l1
password =l2
hashed_passwords =stauth.Hasher(password).generate()
file_path = Path(__file__).parent / "hashed_pw.pkl"
with file_path.open("wb") as file:
pickle.dump(hashed_passwords, file)
elif st.button('Logins'):
names = ['dmin', 'ser']
username =l1
file_path = Path(__file__).parent / 'hashed_pw.pkl'
with file_path.open('rb') as file:
hashed_passwords = pickle.load(file)
authenticator = stauth.Authenticate(names,username,hashed_passwords,'Cheating Detection','abcdefg',cookie_expiry_days=180)
name,authentication_status,username= authenticator.login('Login','main')
if authentication_status == False:
st.error('Username/Password is incorrect')
if authentication_status == None:
st.error('Please enter a username and password')
if authentication_status:
date_time = time.strftime("%b %d %Y %-I:%M %p")
date = date_time.split()
dates = date[0:3]
times = date[3:5]
# x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-xAPPLICACTION -x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x
def non_max_suppression_fast(boxes, overlapThresh):
try:
if len(boxes) == 0:
return []
if boxes.dtype.kind == "i":
boxes = boxes.astype("float")
pick = []
x1 = boxes[:, 0]
y1 = boxes[:, 1]
x2 = boxes[:, 2]
y2 = boxes[:, 3]
area = (x2 - x1 + 1) * (y2 - y1 + 1)
idxs = np.argsort(y2)
while len(idxs) > 0:
last = len(idxs) - 1
i = idxs[last]
pick.append(i)
xx1 = np.maximum(x1[i], x1[idxs[:last]])
yy1 = np.maximum(y1[i], y1[idxs[:last]])
xx2 = np.minimum(x2[i], x2[idxs[:last]])
yy2 = np.minimum(y2[i], y2[idxs[:last]])
w = np.maximum(0, xx2 - xx1 + 1)
h = np.maximum(0, yy2 - yy1 + 1)
overlap = (w * h) / area[idxs[:last]]
idxs = np.delete(idxs, np.concatenate(([last],
np.where(overlap > overlapThresh)[0])))
return boxes[pick].astype("int")
except Exception as e:
print("Exception occurred in non_max_suppression : {}".format(e))
protopath = "MobileNetSSD_deploy.prototxt"
modelpath = "MobileNetSSD_deploy.caffemodel"
detector = cv2.dnn.readNetFromCaffe(prototxt=protopath, caffeModel=modelpath)
# Only enable it if you are using OpenVino environment
# detector.setPreferableBackend(cv2.dnn.DNN_BACKEND_INFERENCE_ENGINE)
# detector.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat",
"bottle", "bus", "car", "cat", "chair", "cow", "diningtable",
"dog", "horse", "motorbike", "person", "pottedplant", "sheep",
"sofa", "train", "tvmonitor"]
tracker = CentroidTracker(maxDisappeared=80, maxDistance=90)
st.markdown(
"""
<style>
[data-testid="stSidebar"][aria-expanded="true"] > div:first-child{
width: 350px
}
[data-testid="stSidebar"][aria-expanded="false"] > div:first-child{
width: 350px
margin-left: -350px
}
</style>
""",
unsafe_allow_html=True,
)
hide_streamlit_style = """
<style>
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
</style>
"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
# Resize Images to fit Container
@st.cache()
# Get Image Dimensions
def image_resize(image, width=None, height=None, inter=cv.INTER_AREA):
dim = None
(h,w) = image.shape[:2]
if width is None and height is None:
return image
if width is None:
r = width/float(w)
dim = (int(w*r),height)
else:
r = width/float(w)
dim = width, int(h*r)
# Resize image
resized = cv.resize(image,dim,interpolation=inter)
return resized
# About Page
authenticator.logout('Logout')
app_mode = st.sidebar.selectbox(
'App Mode',
['About','Application']
)
if app_mode == 'About':
st.title('About Product And Team')
st.markdown('''
Imran Bhai Project
''')
st.markdown(
"""
<style>
[data-testid="stSidebar"][aria-expanded="true"] > div:first-child{
width: 350px
}
[data-testid="stSidebar"][aria-expanded="false"] > div:first-child{
width: 350px
margin-left: -350px
}
</style>
""",
unsafe_allow_html=True,
)
elif app_mode == 'Application':
st.set_option('deprecation.showfileUploaderEncoding', False)
use_webcam = st.button('Use Webcam')
# record = st.sidebar.checkbox("Record Video")
# if record:
# st.checkbox('Recording', True)
# drawing_spec = mp.solutions.drawing_utils.DrawingSpec(thickness=2, circle_radius=1)
# st.sidebar.markdown('---')
# ## Add Sidebar and Window style
# st.markdown(
# """
# <style>
# [data-testid="stSidebar"][aria-expanded="true"] > div:first-child{
# width: 350px
# }
# [data-testid="stSidebar"][aria-expanded="false"] > div:first-child{
# width: 350px
# margin-left: -350px
# }
# </style>
# """,
# unsafe_allow_html=True,
# )
# max_faces = st.sidebar.number_input('Maximum Number of Faces', value=5, min_value=1)
# st.sidebar.markdown('---')
# detection_confidence = st.sidebar.slider('Min Detection Confidence', min_value=0.0,max_value=1.0,value=0.5)
# tracking_confidence = st.sidebar.slider('Min Tracking Confidence', min_value=0.0,max_value=1.0,value=0.5)
# st.sidebar.markdown('---')
## Get Video
stframe = st.empty()
video_file_buffer = st.file_uploader("Upload a Video", type=['mp4', 'mov', 'avi', 'asf', 'm4v'])
temp_file = tempfile.NamedTemporaryFile(delete=False)
if not video_file_buffer:
if use_webcam:
video = cv.VideoCapture(0)
else:
try:
video = cv.VideoCapture(1)
temp_file.name = video
except:
pass
else:
temp_file.write(video_file_buffer.read())
video = cv.VideoCapture(temp_file.name)
width = int(video.get(cv.CAP_PROP_FRAME_WIDTH))
height = int(video.get(cv.CAP_PROP_FRAME_HEIGHT))
fps_input = int(video.get(cv.CAP_PROP_FPS))
## Recording
codec = cv.VideoWriter_fourcc('a','v','c','1')
out = cv.VideoWriter('output1.mp4', codec, fps_input, (width,height))
st.sidebar.text('Input Video')
# st.sidebar.video(temp_file.name)
fps = 0
i = 0
drawing_spec = mp.solutions.drawing_utils.DrawingSpec(thickness=2, circle_radius=1)
kpil, kpil2, kpil3,kpil4,kpil5, kpil6 = st.columns(6)
with kpil:
st.markdown('**Frame Rate**')
kpil_text = st.markdown('0')
with kpil2:
st.markdown('**detection ID**')
kpil2_text = st.markdown('0')
with kpil3:
st.markdown('**Mobile**')
kpil3_text = st.markdown('0')
with kpil4:
st.markdown('**Watch**')
kpil4_text = st.markdown('0')
with kpil5:
st.markdown('**Count**')
kpil5_text = st.markdown('0')
with kpil6:
st.markdown('**Img Res**')
kpil6_text = st.markdown('0')
st.markdown('<hr/>', unsafe_allow_html=True)
# try:
def main():
db = {}
# cap = cv2.VideoCapture('//home//anas//PersonTracking//WebUI//movement.mp4')
path='/usr/local/lib/python3.10/dist-packages/yolo0vs5/yolov5s-int8.tflite'
#count=0
custom = 'yolov5s'
model = torch.hub.load('/usr/local/lib/python3.10/dist-packages/yolovs5', custom, path,source='local',force_reload=True)
b=model.names[0] = 'person'
mobile = model.names[67] = 'cell phone'
watch = model.names[75] = 'clock'
fps_start_time = datetime.datetime.now()
fps = 0
size=416
count=0
counter=0
color=(0,0,255)
cy1=250
offset=6
pt1 = (120, 100)
pt2 = (980, 1150)
color = (0, 255, 0)
pt3 = (283, 103)
pt4 = (1500, 1150)
cy2 = 500
color = (0, 255, 0)
total_frames = 0
prevTime = 0
cur_frame = 0
count=0
counter=0
fps_start_time = datetime.datetime.now()
fps = 0
total_frames = 0
lpc_count = 0
opc_count = 0
object_id_list = []
# success = True
if st.button("Detect"):
try:
while video.isOpened():
ret, frame = video.read()
frame = imutils.resize(frame, width=600)
total_frames = total_frames + 1
(H, W) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(frame, 0.007843, (W, H), 127.5)
detector.setInput(blob)
person_detections = detector.forward()
rects = []
for i in np.arange(0, person_detections.shape[2]):
confidence = person_detections[0, 0, i, 2]
if confidence > 0.5:
idx = int(person_detections[0, 0, i, 1])
if CLASSES[idx] != "person":
continue
person_box = person_detections[0, 0, i, 3:7] * np.array([W, H, W, H])
(startX, startY, endX, endY) = person_box.astype("int")
rects.append(person_box)
boundingboxes = np.array(rects)
boundingboxes = boundingboxes.astype(int)
rects = non_max_suppression_fast(boundingboxes, 0.3)
objects = tracker.update(rects)
for (objectId, bbox) in objects.items():
x1, y1, x2, y2 = bbox
x1 = int(x1)
y1 = int(y1)
x2 = int(x2)
y2 = int(y2)
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 2)
text = "ID: {}".format(objectId)
# print(text)
cv2.putText(frame, text, (x1, y1-5), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1)
if objectId not in object_id_list:
object_id_list.append(objectId)
fps_end_time = datetime.datetime.now()
time_diff = fps_end_time - fps_start_time
if time_diff.seconds == 0:
fps = 0.0
else:
fps = (total_frames / time_diff.seconds)
fps_text = "FPS: {:.2f}".format(fps)
cv2.putText(frame, fps_text, (5, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1)
lpc_count = len(objects)
opc_count = len(object_id_list)
lpc_txt = "LPC: {}".format(lpc_count)
opc_txt = "OPC: {}".format(opc_count)
count += 1
if count % 4 != 0:
continue
# frame=cv.resize(frame, (600,500))
# cv2.line(frame, pt1, pt2,color,2)
# cv2.line(frame, pt3, pt4,color,2)
results = model(frame,size)
components = results.pandas().xyxy[0]
for index, row in results.pandas().xyxy[0].iterrows():
x1 = int(row['xmin'])
y1 = int(row['ymin'])
x2 = int(row['xmax'])
y2 = int(row['ymax'])
confidence = (row['confidence'])
obj = (row['class'])
# min':x1,'ymin':y1,'xmax':x2,'ymax':y2,'confidence':confidence,'Object':obj}
# if lpc_txt is not None:
# try:
# db["student Count"] = [lpc_txt]
# except:
# db["student Count"] = ['N/A']
if obj == 0:
cv2.rectangle(frame,(x1,y1),(x2,y2),(0,0,255),2)
rectx1,recty1 = ((x1+x2)/2,(y1+y2)/2)
rectcenter = int(rectx1),int(recty1)
cx = rectcenter[0]
cy = rectcenter[1]
cv2.circle(frame,(cx,cy),3,(0,255,0),-1)
cv2.putText(frame,str(b), (x1,y1), cv2.FONT_HERSHEY_PLAIN,2,(255,255,255),2)
db["student Count"] = [lpc_txt]
db['Date'] = [date_time]
db['id'] = ['N/A']
db['Mobile']=['N/A']
db['Watch'] = ['N/A']
if cy<(cy1+offset) and cy>(cy1-offset):
DB = []
counter+=1
DB.append(counter)
ff = DB[-1]
fx = str(ff)
# cv2.line(frame, pt1, pt2,(0, 0, 255),2)
# if cy<(cy2+offset) and cy>(cy2-offset):
# cv2.line(frame, pt3, pt4,(0, 0, 255),2)
font = cv2.FONT_HERSHEY_TRIPLEX
cv2.putText(frame,fx,(50, 50),font, 1,(0, 0, 255),2,cv2.LINE_4)
cv2.putText(frame,"Movement",(70, 70),font, 1,(0, 0, 255),2,cv2.LINE_4)
kpil2_text.write(f"<h5 style='text-align: left; color:red;'>{text}</h5>", unsafe_allow_html=True)
db['id'] = [text]
if obj == 67:
cv2.rectangle(frame,(x1,y1),(x2,y2),(0,0,255),2)
rectx1,recty1 = ((x1+x2)/2,(y1+y2)/2)
rectcenter = int(rectx1),int(recty1)
cx = rectcenter[0]
cy = rectcenter[1]
cv2.circle(frame,(cx,cy),3,(0,255,0),-1)
cv2.putText(frame,str(mobile), (x1,y1), cv2.FONT_HERSHEY_PLAIN,2,(255,255,255),2)
cv2.putText(frame,'Mobile',(50, 50),cv2.FONT_HERSHEY_PLAIN, 1,(0, 0, 255),2,cv2.LINE_4)
kpil3_text.write(f"<h5 style='text-align: left; color:red;'>{mobile}{text}</h5>", unsafe_allow_html=True)
db['Mobile']=mobile+' '+text
if obj == 75:
cv2.rectangle(frame,(x1,y1),(x2,y2),(0,0,255),2)
rectx1,recty1 = ((x1+x2)/2,(y1+y2)/2)
rectcenter = int(rectx1),int(recty1)
cx = rectcenter[0]
cy = rectcenter[1]
cv2.circle(frame,(cx,cy),3,(0,255,0),-1)
cv2.putText(frame,str(watch), (x1,y1), cv2.FONT_HERSHEY_PLAIN,2,(255,255,255),2)
cv2.putText(frame,'Watch',(50, 50),cv2.FONT_HERSHEY_PLAIN, 1,(0, 0, 255),2,cv2.LINE_4)
kpil6_text.write(f"<h5 style='text-align: left; color:red;'>{watch}</h5>", unsafe_allow_html=True)
db['Watch']=watch
kpil_text.write(f"<h5 style='text-align: left; color:red;'>{int(fps)}</h5>", unsafe_allow_html=True)
kpil5_text.write(f"<h5 style='text-align: left; color:red;'>{lpc_txt}</h5>", unsafe_allow_html=True)
kpil6_text.write(f"<h5 style='text-align: left; color:red;'>{width*height}</h5>",
unsafe_allow_html=True)
frame = cv.resize(frame,(0,0), fx=0.8, fy=0.8)
frame = image_resize(image=frame, width=640)
stframe.image(frame,channels='BGR', use_column_width=True)
df = pd.DataFrame(db)
df.to_csv('final.csv',mode='a',header=False,index=False)
except:
pass
with open('final.csv') as f:
st.download_button(label = 'Download Cheating Report',data=f,file_name='data.csv')
os.remove("final.csv")
main()
|