Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization in training required #1

Open
up1512001 opened this issue Jun 15, 2022 · 1 comment
Open

Optimization in training required #1

up1512001 opened this issue Jun 15, 2022 · 1 comment

Comments

@up1512001
Copy link
Owner

I thought optimizing training will be good for our project.

@vinay7102001
Copy link
Collaborator

`import cv2
import numpy as np
from keras.models import load_model

import imutils as im

regarding csv file writing

import pandas as pd
import csv
import datetime
import os

def mean_squared_loss(x1, x2): # euclidean distance
difference = x1 - x2
a, b, c, d, e = difference.shape
n_samples = a * b * c * d * e
sq_difference = difference ** 2
Sum = sq_difference.sum()
distance = np.sqrt(Sum)
mean_distance = distance / n_samples # mean squar error
return mean_distance

def help(video):

# csv file handling
file_name = 'E:\\6th_sem\\project\\event detection details.csv'

model = load_model('E:\\6th_sem\\project\\saved_model2.h5')

cap = cv2.VideoCapture(video)
# print(cap.isOpened())
sum = 0
c = 0
while cap.isOpened():
    imagedump = []
    ret, frame = cap.read()
    for i in range(10):
        ret, frame = cap.read()
        if ret == False:
            break
        image = im.resize(frame, width=1000, height=1000)
        frame = cv2.resize(frame, (227, 227), interpolation=cv2.INTER_AREA)
        gray = 0.2989 * frame[:, :, 0] + 0.5870 * frame[:, :, 1] + 0.1140 * frame[:, :, 2]  # convert into gray scale
        gray = (gray - gray.mean()) / gray.std()
        gray = np.clip(gray, 0, 1)
        imagedump.append(gray)
    imagedump = np.array(imagedump)
    imagedump.resize(227, 227, 10)
    imagedump = np.expand_dims(imagedump, axis=0)  # 3d data converted into 4d
    imagedump = np.expand_dims(imagedump, axis=4)  # 4d data into 5d
    output = model.predict(imagedump)
    loss = mean_squared_loss(imagedump, output)
    if ret == False:
        print("video end")
        break
    if cv2.waitKey(10) & 0xFF == ord('q'):
        break

    current_date = datetime.date.today()
    # print(current_date)
    current_time = datetime.datetime.now()
    current_time = current_time.strftime('%H:%M:%S')
    # print(current_time)

    print(loss)

    if loss > 0.00039:
        rows = [[f'{current_date}', f'{current_time}', f'{video[-1:-7:-1][::-1]}', 'Abnormal']]
        # regarding csv file writing
        with open(file_name, 'a') as csvfile:
            csvwriter = csv.writer(csvfile)
            # csvwriter.writerow(fields)
            csvwriter.writerows(rows)
        print('Abnormal Event Detected')
        cv2.putText(image, "abnormal event", (100, 80), cv2.FONT_HERSHEY_TRIPLEX, 2, (250, 24, 255), 4)
    else:
        cv2.putText(image, "normal event", (100, 80), cv2.FONT_HERSHEY_TRIPLEX, 2, (250, 24, 255), 4)
    cv2.imshow("video", image)
cap.release()
cv2.destroyAllWindows()

if name == 'main':
train_video_path = 'E:\6th_sem\project\testing_videos\07.avi'
# train_video_path = 'E:\6th_sem\project\testing_videos\13.avi'
# train_video_path = 'E:\6th_sem\project\testing_videos\12.avi'
# train_video_path = 'E:\6th_sem\project\testing_videos\06.avi'
# train_video_path = 'E:\6th_sem\project\testing_videos\05.avi'
# videos = os.listdir(train_video_path)
# print(videos)
# for video in videos:
# if video.endswith('.mp4') or video.endswith('.avi'):
help(train_video_path)`

@up1512001 up1512001 reopened this Jun 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants