编写测试类,创建一个Goods类对象“explosive”,并设置该物品是违禁品,然后模拟检测到该物品是违禁品。
时间: 2024-05-22 17:12:22 浏览: 15
public class GoodsTest {
public static void main(String[] args) {
Goods explosive = new Goods("explosive");
explosive.setContraband(true);
boolean result = explosive.check();
if(result){
System.out.println("检测到该物品是违禁品");
}else{
System.out.println("该物品非违禁品");
}
}
}
相关问题
写一个用深度学习进行危险品检测的代码
好的,这里提供一个基于深度学习的危险品检测代码,使用的是TensorFlow框架和YOLOv3模型。
首先,需要安装相关的依赖库:
```python
!pip install tensorflow==2.4.1
!pip install opencv-python
!pip install numpy
```
接下来,加载模型和标签文件:
```python
import cv2
import numpy as np
import tensorflow as tf
# 加载模型和标签文件
model = tf.keras.models.load_model('path/to/model.h5')
labels = ['explosive', 'flammable', 'toxic']
```
然后,定义一个函数,用于将模型输出的坐标转换成实际物体的位置和大小:
```python
def convert_coordinates(image, box):
height, width, _ = image.shape
x, y, w, h = box
left = int((x - w / 2) * width)
top = int((y - h / 2) * height)
right = int((x + w / 2) * width)
bottom = int((y + h / 2) * height)
return left, top, right, bottom
```
最后,定义一个函数,用于对图像进行危险品检测:
```python
def detect_dangerous_goods(image):
# 对图像进行预处理
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, (416, 416))
image = image / 255.0
image = np.expand_dims(image, axis=0)
# 使用模型进行预测
predictions = model.predict(image)
# 解析预测结果
boxes, scores, classes = tf.image.combined_non_max_suppression(
boxes=tf.reshape(predictions[:, :, :4], (1, -1, 4)),
scores=tf.reshape(predictions[:, :, 4], (1, -1)),
max_output_size_per_class=10,
max_total_size=10,
iou_threshold=0.5,
score_threshold=0.5
)
# 显示检测结果
image = cv2.cvtColor(image[0], cv2.COLOR_RGB2BGR)
for i in range(len(boxes[0])):
if int(classes[0][i]) < len(labels):
left, top, right, bottom = convert_coordinates(image, boxes[0][i].numpy())
cv2.rectangle(image, (left, top), (right, bottom), (0, 255, 0), 2)
label = labels[int(classes[0][i])]
cv2.putText(image, label, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
return image
```
使用下面的代码进行测试:
```python
# 读取图像
image = cv2.imread('path/to/image.jpg')
# 进行检测
result = detect_dangerous_goods(image)
# 显示结果
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
注意,这个代码只是一个简单的示例,实际应用中需要根据具体的需求进行调整和优化。另外,这个代码只针对单张图像进行检测,如果需要对视频流进行检测,则需要对代码进行进一步的修改。
基于yolo v5的危险物品检测代码
以下是使用Yolo v5进行危险物品检测的示例代码:
```python
import torch
import cv2
import numpy as np
from pathlib import Path
# Load YOLOv5s model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
# Define class names
classes = ['gun', 'knife', 'explosive']
# Load image
img_path = Path('path/to/image')
img = cv2.imread(str(img_path))
# Detect objects
results = model(img)
# Filter out dangerous objects
dangerous_results = []
for result in results.xyxy[0]:
if result[5] in [classes.index(c) for c in classes]:
dangerous_results.append(result)
# Draw bounding boxes
for result in dangerous_results:
x1, y1, x2, y2, _, cls = result
cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 2)
cv2.putText(img, classes[int(cls)], (int(x1), int(y1)-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
# Show image
cv2.imshow('Dangerous Objects Detection', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在这个示例中,我们首先加载了Yolo v5S的预训练模型。然后定义了我们要检测的类别名称。接着读取了一张图片并使用模型进行检测。最后,我们过滤出了危险物品的检测结果,并在图片上绘制了边界框和类别标签。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)