import cv2
import ultralytics
from ultralytics import YOLO
from PIL import Image, ImageFile
import requests
from io import BytesIO
import pandas as pd
import time
import functions
import os
ImageFile.LOAD_TRUNCATED_IMAGES = True
pd.options.display.max_columns = 21
v3: 0: ‘awb’, 1: ‘awb-njv’, 2: ‘weighing-platform-bulky’, 3: ‘weighing-platform-small’
def predicted_lzd_gcs(model3):
awb =
ci_awb =
platform =
ci_platform =
ids =
predicted_value =
reason =
count = 0
# Open the webcam for capturing frames
cap = cv2.VideoCapture(0) # 0 represents the default webcam
try:
while True:
count = count + 1
print(count)
functions.stop(count)
# Capture a frame from the webcam
ret, frame = cap.read()
# Convert the frame to a PIL Image
source = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
results = model3(source)
# Process the detected objects and update your data accordingly
for box in results[0].boxes:
cls = box.cls
conf = box.conf
# Your object analysis logic goes here...
# Example: Check if cls is a certain class and update your data accordingly
if cls == 0:
awb.append(1)
ci_awb.append(conf)
elif cls in [2, 3]:
platform.append(1)
ci_platform.append(conf)
# Display the processed frame (optional)
cv2.imshow("Processed Frame", cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
# Press 'q' to exit the loop
if cv2.waitKey(1) & 0xFF == ord('q'):
break
finally:
# Release the webcam and close any open windows
cap.release()
cv2.destroyAllWindows()
# Rest of your code to create the DataFrame and return it...
def recommend_lzd_gcs(pred, ci_awb, platform, ci_platform):
if pred == ‘pass’:
if min(ci_awb) < 0.60:
return 1
elif 1 in platform and min(ci_platform) < 0.956:
return 1
else:
return 0
elif pred == 'fail':
return 1
#-----------------------------------------------------------------------------------------------------#
LZD - Drive
def predicted_lzd_drive(model):
awb =
ci_awb =
platform =
ci_platform =
ids =
predicted_value =
reason =
orders =
# Open the webcam for capturing frames
cap = cv2.VideoCapture(0) # 0 represents the default webcam
try:
while True:
# Capture a frame from the webcam
ret, frame = cap.read()
# Convert the frame to a PIL Image
source = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
results = model(source)
cls4 = []
ci_cls4 = []
# Rest of your code to process and analyze the frame goes here...
# Press 'q' to exit the loop
if cv2.waitKey(1) & 0xFF == ord('q'):
break
finally:
# Release the webcam and close any open windows
cap.release()
cv2.destroyAllWindows()
# Rest of your code to create the DataFrame and return it...
def recommend_lzd_drive(pred, ci_awb, platform, ci_platform):
if pred == ‘pass’:
if ci_awb < float(0.60):
return 1
elif platform == 1 and ci_platform < float(0.956):
return 1
else:
return 0
elif pred == 'fail':
return 1
#-----------------------------------------------------------------------------------------------------#
Non - partnership
def predicted_npsp(folder_pth, model):
platform =
orders =
ci_platform =
ids =
predicted_value =
reason =
for i in os.listdir(folder_pth):
id_path = f’{folder_pth}/{i}’
print(id_path)
for j in os.listdir(id_path):
name = j.split('.')[0]
length = len(name.split('_'))
id = name.split('_')[0]
order = name.split('_')[length-1]
source = f'{id_path}/{j}'
results = model(source)
cls4 = []
ci_cls4 = []
# Predict Fail or Pass
if str(results[0].boxes.cls) == 'tensor([])':
reason.append('invisible')
predicted_value.append('fail')
else:
for box in results[0].boxes:
cls = box.cls
ci = box.conf
# Do not accept awb and weighing platform of confident interval of awb and weighing
# platform less than 64% and 70% respectively.
cls4.append(int(cls))
ci_cls4.append(float(ci))
if functions.unique(cls4) == [0]:
reason.append('strange stuffs')
elif functions.unique(cls4) == [1]:
reason.append('strange stuffs')
else:
reason.append('')
if functions.unique(cls4) == [0, 2] or functions.unique(cls4) == [0, 3]:
predicted_value.append('pass')
elif functions.unique(cls4) == [2] or functions.unique(cls4) == [3]:
predicted_value.append('pass')
else:
predicted_value.append('fail')
# Input class and confident interval
model4_dict = functions.m_dict_5(cls4, ci_cls4)
model4_dict = functions.transform_dict_5(model4_dict)
if model4_dict[2] > float(0) or model4_dict[3] > float(0):
platform.append(1)
if model4_dict[2] > float(0):
ci_platform.append(model4_dict[2])
elif model4_dict[3] > float(0):
ci_platform.append(model4_dict[3])
else:
platform.append(0)
ci_platform.append(0.0)
ids.append(id)
orders.append(order)
print(reason)
cm = pd.DataFrame(list(zip(ids, orders, predicted_value, reason, platform, ci_platform)),
columns=['TID', 'Photo_ID', 'Predicted_Value', 'Fail_Reason',
'Weighing_Platform', 'Conf_Weighing_Platform'])
return cm
def recommend_npsp(pred, platform, ci_platform):
if pred == ‘pass’:
if platform == 1 and ci_platform < float(0.948):
return 1
else:
return 0
elif pred == 'fail':
return 1
#------------------------------------------------appended---------------------------------------#
Initialize your object detection model (replace with your model)
model3 = YOLO(“standard_image_v3.pt”)
Define a function to perform object detection and analysis
def perform_object_detection():
# Call the ‘predicted_lzd_gcs’ function with your object detection model
result = predicted_lzd_gcs(model3)
print(result)
Run object detection (e.g., when the script is executed)
if name == “main”:
perform_object_detection()