MTerryJack commited on
Commit
cbce3da
·
verified ·
1 Parent(s): 904258e

Upload 8 files

Browse files
Files changed (9) hide show
  1. .gitattributes +1 -0
  2. config.json +142 -0
  3. element.yaml +6 -0
  4. example.jpg +3 -0
  5. main.py +111 -0
  6. preprocessor_config.json +25 -0
  7. pyproject.toml +10 -0
  8. pytorch_model.bin +3 -0
  9. uv.lock +0 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ example.jpg filter=lfs diff=lfs merge=lfs -text
config.json ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "facebook/detr-resnet-50",
3
+ "activation_dropout": 0.0,
4
+ "activation_function": "relu",
5
+ "architectures": [
6
+ "DetrForObjectDetection"
7
+ ],
8
+ "attention_dropout": 0.0,
9
+ "auxiliary_loss": false,
10
+ "backbone": null,
11
+ "backbone_config": {
12
+ "_name_or_path": "",
13
+ "add_cross_attention": false,
14
+ "architectures": null,
15
+ "bad_words_ids": null,
16
+ "begin_suppress_tokens": null,
17
+ "bos_token_id": null,
18
+ "chunk_size_feed_forward": 0,
19
+ "cross_attention_hidden_size": null,
20
+ "decoder_start_token_id": null,
21
+ "depths": [
22
+ 3,
23
+ 4,
24
+ 6,
25
+ 3
26
+ ],
27
+ "diversity_penalty": 0.0,
28
+ "do_sample": false,
29
+ "downsample_in_first_stage": false,
30
+ "early_stopping": false,
31
+ "embedding_size": 64,
32
+ "encoder_no_repeat_ngram_size": 0,
33
+ "eos_token_id": null,
34
+ "exponential_decay_length_penalty": null,
35
+ "finetuning_task": null,
36
+ "forced_bos_token_id": null,
37
+ "forced_eos_token_id": null,
38
+ "hidden_act": "relu",
39
+ "hidden_sizes": [
40
+ 256,
41
+ 512,
42
+ 1024,
43
+ 2048
44
+ ],
45
+ "id2label": {
46
+ "0": "LABEL_0",
47
+ "1": "LABEL_1"
48
+ },
49
+ "is_decoder": false,
50
+ "is_encoder_decoder": false,
51
+ "label2id": {
52
+ "LABEL_0": 0,
53
+ "LABEL_1": 1
54
+ },
55
+ "layer_type": "bottleneck",
56
+ "length_penalty": 1.0,
57
+ "max_length": 20,
58
+ "min_length": 0,
59
+ "model_type": "resnet",
60
+ "no_repeat_ngram_size": 0,
61
+ "num_beam_groups": 1,
62
+ "num_beams": 1,
63
+ "num_channels": 3,
64
+ "num_return_sequences": 1,
65
+ "out_features": [
66
+ "stage4"
67
+ ],
68
+ "out_indices": [
69
+ 4
70
+ ],
71
+ "output_attentions": false,
72
+ "output_hidden_states": false,
73
+ "output_scores": false,
74
+ "pad_token_id": null,
75
+ "prefix": null,
76
+ "problem_type": null,
77
+ "pruned_heads": {},
78
+ "remove_invalid_values": false,
79
+ "repetition_penalty": 1.0,
80
+ "return_dict": true,
81
+ "return_dict_in_generate": false,
82
+ "sep_token_id": null,
83
+ "stage_names": [
84
+ "stem",
85
+ "stage1",
86
+ "stage2",
87
+ "stage3",
88
+ "stage4"
89
+ ],
90
+ "suppress_tokens": null,
91
+ "task_specific_params": null,
92
+ "temperature": 1.0,
93
+ "tf_legacy_loss": false,
94
+ "tie_encoder_decoder": false,
95
+ "tie_word_embeddings": true,
96
+ "tokenizer_class": null,
97
+ "top_k": 50,
98
+ "top_p": 1.0,
99
+ "torch_dtype": null,
100
+ "torchscript": false,
101
+ "typical_p": 1.0,
102
+ "use_bfloat16": false
103
+ },
104
+ "bbox_cost": 5,
105
+ "bbox_loss_coefficient": 5,
106
+ "class_cost": 1,
107
+ "d_model": 256,
108
+ "decoder_attention_heads": 8,
109
+ "decoder_ffn_dim": 2048,
110
+ "decoder_layerdrop": 0.0,
111
+ "decoder_layers": 6,
112
+ "dice_loss_coefficient": 1,
113
+ "dilation": null,
114
+ "dropout": 0.1,
115
+ "encoder_attention_heads": 8,
116
+ "encoder_ffn_dim": 2048,
117
+ "encoder_layerdrop": 0.0,
118
+ "encoder_layers": 6,
119
+ "eos_coefficient": 0.1,
120
+ "giou_cost": 2,
121
+ "giou_loss_coefficient": 2,
122
+ "id2label": {
123
+ "0": "LABEL_0"
124
+ },
125
+ "init_std": 0.02,
126
+ "init_xavier_std": 1.0,
127
+ "is_encoder_decoder": true,
128
+ "label2id": {
129
+ "LABEL_0": 0
130
+ },
131
+ "mask_loss_coefficient": 1,
132
+ "model_type": "detr",
133
+ "num_channels": 3,
134
+ "num_hidden_layers": 6,
135
+ "num_queries": 100,
136
+ "position_embedding_type": "sine",
137
+ "scale_embedding": false,
138
+ "torch_dtype": "float32",
139
+ "transformers_version": "4.34.0",
140
+ "use_pretrained_backbone": null,
141
+ "use_timm_backbone": false
142
+ }
element.yaml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ version: 0.1.0
2
+ element_type: Detect
3
+ main: main.py
4
+ source: https://huggingface.co/journey2001/qr_detect_300ep
5
+ objects:
6
+ - qr_code
example.jpg ADDED

Git LFS Details

  • SHA256: 66716a2014d9a49ca86bb5dfec99095c5303d1ff63808f2804ef88cdd05d22cd
  • Pointer size: 131 Bytes
  • Size of remote file: 529 kB
main.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import argparse
4
+ import json
5
+ import sys
6
+ from io import BytesIO
7
+ from pathlib import Path
8
+ from typing import Any, Dict, List, Tuple
9
+
10
+ import numpy as np
11
+ from PIL import Image
12
+ import torch
13
+ from transformers import AutoImageProcessor, DetrForObjectDetection
14
+
15
+
16
+ ModelBundle = Tuple[DetrForObjectDetection, AutoImageProcessor]
17
+
18
+
19
+ def load_image(frame: Any, base_dir: Path) -> Image.Image:
20
+ if isinstance(frame, (bytes, bytearray, memoryview)):
21
+ return Image.open(BytesIO(frame)).convert("RGB")
22
+
23
+ path = Path(str(frame))
24
+ if not path.is_absolute():
25
+ path = (Path.cwd() / path).resolve()
26
+ if not path.exists():
27
+ candidate = (base_dir / str(frame)).resolve()
28
+ if candidate.exists():
29
+ path = candidate
30
+ return Image.open(path).convert("RGB")
31
+
32
+
33
+ def load_model(*_args: Any, **_kwargs: Any) -> ModelBundle | None:
34
+ base_dir = Path(__file__).resolve().parent
35
+ if not (base_dir / "config.json").exists():
36
+ return None
37
+
38
+ processor = AutoImageProcessor.from_pretrained(str(base_dir))
39
+ model = DetrForObjectDetection.from_pretrained(str(base_dir))
40
+ model.eval()
41
+ return model, processor
42
+
43
+
44
+ def run_model(model_bundle: ModelBundle, frame: "np.ndarray") -> List[Dict[str, Any]]:
45
+ image = Image.fromarray(frame)
46
+ model, processor = model_bundle
47
+
48
+ inputs = processor(images=image, return_tensors="pt")
49
+ with torch.no_grad():
50
+ outputs = model(**inputs)
51
+
52
+ target_sizes = torch.tensor([image.size[::-1]])
53
+ results = processor.post_process_object_detection(
54
+ outputs,
55
+ threshold=0.5,
56
+ target_sizes=target_sizes,
57
+ )[0]
58
+
59
+ detections: List[Dict[str, Any]] = []
60
+ names = model.config.id2label or {}
61
+ name_overrides = {"LABEL_0": "qr_code"}
62
+
63
+ for det_idx, (score, label, box) in enumerate(
64
+ zip(results["scores"], results["labels"], results["boxes"])
65
+ ):
66
+ class_id = int(label.item())
67
+ class_name = names.get(class_id, str(class_id))
68
+ class_name = name_overrides.get(class_name, class_name)
69
+ xyxy = [float(x) for x in box.tolist()]
70
+ detections.append(
71
+ {
72
+ "frame_idx": 0,
73
+ "class": class_name,
74
+ "bbox": xyxy,
75
+ "score": float(score.item()),
76
+ "track_id": f"f0-d{det_idx}",
77
+ }
78
+ )
79
+
80
+ return detections
81
+
82
+
83
+ def build_parser() -> argparse.ArgumentParser:
84
+ parser = argparse.ArgumentParser(description="Run QR detect element.")
85
+ parser.add_argument(
86
+ "--stdin-raw",
87
+ action="store_true",
88
+ default=True,
89
+ help="Read raw image bytes from stdin.",
90
+ )
91
+ return parser
92
+
93
+
94
+ if __name__ == "__main__":
95
+ build_parser().parse_args()
96
+
97
+ base_dir = Path(__file__).resolve().parent
98
+ model_bundle = load_model()
99
+ if model_bundle is None:
100
+ print("[]")
101
+ sys.exit(0)
102
+
103
+ try:
104
+ image = load_image(sys.stdin.buffer.read(), base_dir)
105
+ except Exception:
106
+ print("[]")
107
+ sys.exit(0)
108
+
109
+ frame = np.array(image)
110
+ output = run_model(model_bundle, frame)
111
+ print(json.dumps(output))
preprocessor_config.json ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "do_normalize": true,
3
+ "do_pad": true,
4
+ "do_rescale": true,
5
+ "do_resize": true,
6
+ "feature_extractor_type": "DetrFeatureExtractor",
7
+ "format": "coco_detection",
8
+ "image_mean": [
9
+ 0.485,
10
+ 0.456,
11
+ 0.406
12
+ ],
13
+ "image_processor_type": "DetrImageProcessor",
14
+ "image_std": [
15
+ 0.229,
16
+ 0.224,
17
+ 0.225
18
+ ],
19
+ "resample": 2,
20
+ "rescale_factor": 0.00392156862745098,
21
+ "size": {
22
+ "longest_edge": 1333,
23
+ "shortest_edge": 800
24
+ }
25
+ }
pyproject.toml ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ name = "qr-detect-300ep"
3
+ version = "0.1.0"
4
+ requires-python = ">=3.11"
5
+ dependencies = [
6
+ "numpy>=1.26",
7
+ "pillow>=10.0",
8
+ "torch>=2.2",
9
+ "transformers>=4.38",
10
+ ]
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3b5eff261ebe4f1048560c50df8bfe1584aff25870e0fff4035e8c829b7c8358
3
+ size 166616594
uv.lock ADDED
The diff for this file is too large to render. See raw diff