attempt包测试3_Adverbs_2020-02-04

attempt包测试3_Adverbs_20200204Tuesday

1.设置当前工作目录

getwd()
setwd("./attempt/")

2.导入R包

library(attempt)

3.测试:Adverbs take a function and return a modified function.

3.1 silently()

#@ silently() transforms a function so that when you call this new function, it returns nothing unless there is an error or a warning (contrary to attempt that returns the result). In a sense, the new function stay silent unless error or warning.
silent_log <- silently(log)
silent_log(1)
silent_log(“a”)

Error in .f(…) : 数学函数中用了非数值参数

#@ With silently(), the result is never returned.
silent_matrix <- silently(matrix)
silent_matrix(1:3,2)

Warning message:

In .f(…) : 数据长度[3]不是矩阵行数[2]的整倍

3.2 surely()

#@ surely() transforms a function so that when you call this new function, it calls attempt() - i.e. in the code below, calling sure_log(1) is the same as calling attempt(log(1)). In a sense, you’re sure this new function will always work.
sure_log <- surely(log)
sure_log(1)

[1] 0

sure_log(“a”)

Error: 数学函数中用了非数值参数

3.3 with_message() and with_warning()

#@ These two functions take a function, and add a warning or a message to it.
as_num_msg <- with_message(as.numeric, msg = “We’re performing a numeric conversion”)
as_num_warn <- with_warning(as.numeric, msg = “We’re performing a numeric conversion”)
as_num_msg(“1”)

We’re performing a numeric conversion

[1] 1

as_num_warn(“1”)

[1] 1

Warning message:

In as_num_warn(“1”) : We’re performing a numeric conversion

3.4 without_message(), without_warning(), and discretly()

#@ These three functions do the opposite, as they remove warnings and messages:
matrix(1:3, ncol = 2)

[,1] [,2]

[1,] 1 3

[2,] 2 1

Warning message:

In matrix(1:3, ncol = 2) : 数据长度[3]不是矩阵行数[2]的整倍

no_warning_matrix <- without_warning(matrix)
no_warning_matrix(1:3, ncol = 2)

[,1] [,2]

[1,] 1 3

[2,] 2 1

4.结束

sessionInfo()

R version 3.6.2 (2019-12-12)

Platform: x86_64-w64-mingw32/x64 (64-bit)

Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:

[1] LC_COLLATE=Chinese (Simplified)_China.936

[2] LC_CTYPE=Chinese (Simplified)_China.936

[3] LC_MONETARY=Chinese (Simplified)_China.936

[4] LC_NUMERIC=C

[5] LC_TIME=Chinese (Simplified)_China.936

attached base packages:

[1] stats4 parallel stats graphics grDevices utils

[7] datasets methods base

other attached packages:

[1] attempt_0.3.0 Biostrings_2.54.0 XVector_0.26.0

[4] IRanges_2.20.2 S4Vectors_0.24.2 BiocGenerics_0.32.0

loaded via a namespace (and not attached):

[1] Seurat_3.1.2 TH.data_1.0-10

[3] Rtsne_0.15 colorspace_1.4-1

[5] seqinr_3.6-1 pryr_0.1.4

[7] ggridges_0.5.2 rstudioapi_0.10

[9] leiden_0.3.2 listenv_0.8.0

[11] npsurv_0.4-0 ggrepel_0.8.1

[13] alakazam_0.3.0 mvtnorm_1.0-12

[15] codetools_0.2-16 splines_3.6.2

[17] R.methodsS3_1.7.1 mnormt_1.5-5

[19] lsei_1.2-0 TFisher_0.2.0

[21] zeallot_0.1.0 ade4_1.7-13

[23] jsonlite_1.6 packrat_0.5.0

[25] ica_1.0-2 cluster_2.1.0

[27] png_0.1-7 R.oo_1.23.0

[29] uwot_0.1.5 sctransform_0.2.1

[31] readr_1.3.1 compiler_3.6.2

[33] httr_1.4.1 backports_1.1.5

[35] assertthat_0.2.1 Matrix_1.2-18

[37] lazyeval_0.2.2 htmltools_0.4.0

[39] prettyunits_1.1.0 tools_3.6.2

[41] rsvd_1.0.2 igraph_1.2.4.2

[43] gtable_0.3.0 glue_1.3.1

[45] RANN_2.6.1 reshape2_1.4.3

[47] dplyr_0.8.3 Rcpp_1.0.3

[49] Biobase_2.46.0 vctrs_0.2.1

[51] multtest_2.42.0 gdata_2.18.0

[53] ape_5.3 nlme_3.1-142

[55] gbRd_0.4-11 lmtest_0.9-37

[57] stringr_1.4.0 globals_0.12.5

[59] lifecycle_0.1.0 irlba_2.3.3

[61] gtools_3.8.1 future_1.16.0

[63] zlibbioc_1.32.0 MASS_7.3-51.4

[65] zoo_1.8-7 scales_1.1.0

[67] hms_0.5.3 sandwich_2.5-1

[69] RColorBrewer_1.1-2 reticulate_1.14

[71] pbapply_1.4-2 gridExtra_2.3

[73] ggplot2_3.2.1 stringi_1.4.3

[75] mutoss_0.1-12 plotrix_3.7-7

[77] caTools_1.17.1.4 bibtex_0.4.2.2

[79] Rdpack_0.11-1 SDMTools_1.1-221.2

[81] rlang_0.4.2 pkgconfig_2.0.3

[83] bitops_1.0-6 lattice_0.20-38

[85] ROCR_1.0-7 purrr_0.3.3

[87] htmlwidgets_1.5.1 cowplot_1.0.0

[89] tidyselect_0.2.5 RcppAnnoy_0.0.14

[91] plyr_1.8.5 magrittr_1.5

[93] R6_2.4.1 gplots_3.0.1.2

[95] multcomp_1.4-12 pillar_1.4.3

[97] sn_1.5-4 fitdistrplus_1.0-14

[99] survival_3.1-8 tibble_2.1.3

[101] future.apply_1.4.0 tsne_0.1-3

[103] crayon_1.3.4 KernSmooth_2.23-16

[105] plotly_4.9.1 progress_1.2.2

[107] grid_3.6.2 data.table_1.12.8

[109] metap_1.2 digest_0.6.23

[111] tidyr_1.0.0 numDeriv_2016.8-1.1

[113] R.utils_2.9.2 RcppParallel_4.4.4

[115] munsell_0.5.0 viridisLite_0.3.0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据您提供的代码,我重新整理了Detect.py的代码,并添加了时间记录和报警规避功能的注释说明。以下是修改后的代码: ```python import cv2 import time import argparse from models.experimental import attempt_load from utils.datasets import letterbox from utils.general import non_max_suppression, scale_coords, xyxy2xywh from utils.torch_utils import select_device # 解析命令行参数 parser = argparse.ArgumentParser() parser.add_argument('--weights', nargs='+', type=str, default='yolov5s.pt', help='model.pt path(s)') parser.add_argument('--source', type=str, default='data/images', help='source') # file/folder, 0 for webcam parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)') parser.add_argument('--conf-thres', type=float, default=0.25, help='object confidence threshold') parser.add_argument('--iou-thres', type=float, default=0.45, help='IOU threshold for NMS') parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu') parser.add_argument('--view-img', action='store_true', help='display results') parser.add_argument('--save-txt', action='store_true', help='save results to *.txt') parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels') args = parser.parse_args() # 加载模型权重 device = select_device(args.device) # 使用指定设备 model = attempt_load(args.weights, map_location=device) # 加载模型 # 初始化时间记录和报警规避相关变量 last_alarm_time = time.time() # 上次报警时间 alarm_interval = 10 # 报警时间间隔(秒) # 打开摄像头 cap = cv2.VideoCapture(args.source) while cap.isOpened(): ret, frame = cap.read() if not ret: break # 图像预处理 img = letterbox(frame, new_shape=args.img_size)[0] img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB img = np.ascontiguousarray(img) # 将图像转换为Tensor并进行推理 img = torch.from_numpy(img).to(device) img = img.float() / 255.0 if img.ndimension() == 3: img = img.unsqueeze(0) # 推理并进行非极大值抑制 pred = model(img, augment=False)[0] pred = non_max_suppression(pred, args.conf_thres, args.iou_thres) # 处理检测结果 for det in pred[0]: if det is not None: # 获取检测框的坐标和置信度 det[:, :4] = scale_coords(img.shape[2:], det[:, :4], frame.shape).round() for *xyxy, conf, cls in reversed(det): x, y, w, h = map(int, xyxy2xywh(xyxy)) label = f'{names[int(cls)]} {conf:.2f}' # 判断是否检测到不带安全帽的人 if label == 'no_hat': # 检查是否达到报警时间间隔 current_time = time.time() if current_time - last_alarm_time > alarm_interval: # 记录报警时间并执行报警操作 last_alarm_time = current_time print("Alarm: No helmet detected!") # 添加报警操作的代码 # 在图像上绘制检测结果 cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) cv2.putText(frame, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2) # 显示检测结果 cv2.imshow('Detection', frame) if cv2.waitKey(1) == ord('q'): break cap.release() cv2.destroyAllWindows() ``` 请注意,上述代码仅修改了与您提供的代码不一致的部分,并添加了时间记录和报警规避功能的注释说明。您可能需要根据您的实际需求对其进行进一步的修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值