-
Notifications
You must be signed in to change notification settings - Fork 3
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
Created FakeDetector and FakeGroundTruthDatareader, need to revise datareaders structure and layout(groundtruth) file format #5
Open
Serebro1
wants to merge
7
commits into
itlab-vision:main
Choose a base branch
from
Serebro1:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+287
−5
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f5ba254
Add visualizer, datareader, pseudo-main
Serebro1 5f96140
Update GUI & add pseudo detector
Serebro1 7a4a98e
Code refactor
Serebro1 4a15d93
fixes
Serebro1 3bb242f
Merge branch 'main' of https://github.com/Serebro1/vehicle-detection
Serebro1 0c37522
Create FakeDetector and FakeGroundTruthDatareader, need to revise dat…
Serebro1 7f12459
Fix FakeGroundtruthReader, Visualizer.
Serebro1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import sys | ||
import os | ||
|
||
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) | ||
sys.path.append(project_root) | ||
|
||
from src.gui_application.visualizer import Visualize | ||
from src.utils.data_reader import GroundtruthReader, FakeGroundtruthReader | ||
from src.utils.frame_data_reader import FrameDataReader | ||
from src.vehicle_detector.detector import Detector | ||
import datetime | ||
import argparse | ||
import os | ||
from PIL import Image | ||
|
||
import cv2 as cv | ||
import numpy as np | ||
|
||
def cli_argument_parser(): | ||
parser = argparse.ArgumentParser() | ||
|
||
parser.add_argument('-t', '--mode', | ||
help='Mode (\'image\', \'video\')', | ||
type=str, | ||
dest='mode', | ||
choices=['image', 'video'], | ||
required=True) | ||
parser.add_argument('-v', '--video', | ||
help='Path to a video file', | ||
type=str, | ||
dest='video_path') | ||
parser.add_argument('-i', '--image', | ||
help='Path to images', | ||
type=str, | ||
dest='images_path') | ||
parser.add_argument('-g', '--groundtruth', | ||
help='Path to a file of groundtruth', | ||
type=str, | ||
dest='groundtruth_path', | ||
required=False) | ||
parser.add_argument('-m', '--model', | ||
help='Path to a model', | ||
type=str, | ||
dest='model_path', | ||
required=True, | ||
default=None) | ||
|
||
|
||
args = parser.parse_args() | ||
return args | ||
|
||
#некоторое подобие main | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Удалить такой комментарий :) |
||
def main(): | ||
args = cli_argument_parser() | ||
reader = FrameDataReader.create( args.mode, (args.video_path or args.images_path) ) | ||
adapter = None | ||
detector = Detector.create( "fake" ) | ||
visualizer = Visualize( reader, detector, FakeGroundtruthReader().read(args.groundtruth_path) ) | ||
visualizer.show() | ||
|
||
if __name__ == '__main__': | ||
main() |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import cv2 as cv | ||
from ..utils.frame_data_reader import FrameDataReader | ||
from ..utils.data_reader import GroundtruthReader, FakeGroundtruthReader | ||
from ..vehicle_detector.fake_detector import FakeDetector | ||
import random | ||
class Visualize: | ||
def __init__(self, datareader:FrameDataReader, detector:FakeDetector, gt_data:list): | ||
self.datareader = datareader | ||
self.detector = detector | ||
self.gt_layout = gt_data | ||
def show(self): | ||
try: | ||
frame_idx = 0 | ||
for image in self.datareader: | ||
if image is None: | ||
break | ||
|
||
for box in self.detector.detect(image): | ||
self.__draw_box(image, box, (255, 0, 0)) | ||
|
||
if self.gt_layout: | ||
for box in self.__get_groundtruth_bboxes(frame_idx): | ||
self.__draw_box(image, box, (0, 255, 0)) | ||
|
||
frame_idx+=1 | ||
if cv.waitKey(25) & 0xFF == ord('q'): | ||
break | ||
except Exception as e: | ||
print(f"An error occurred: {e}") | ||
finally: | ||
cv.destroyAllWindows() | ||
|
||
def __draw_box(self, image, box, color): | ||
label, x1, y1, x2, y2 = box | ||
cv.rectangle(image, (x1, y1), (x2, y2), color, 2) | ||
cv.putText(image, label, (x1, y1 - 10), cv.FONT_HERSHEY_SIMPLEX, 0.5, color, 2) | ||
cv.imshow("Image", image) | ||
|
||
def __get_groundtruth_bboxes(self, frame_idx): | ||
return [item[1:] for item in self.gt_layout if item[0] == frame_idx] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import cv2 as cv | ||
import os | ||
from abc import ABC, abstractmethod | ||
|
||
class FrameDataReader(ABC): | ||
@abstractmethod | ||
def __iter__(self): | ||
pass | ||
|
||
@abstractmethod | ||
def __next__(self): | ||
pass | ||
|
||
@staticmethod | ||
def create(mode, dir_path): | ||
if mode == "video": | ||
return VideoDataReader(dir_path) | ||
elif mode == "image": | ||
return ImgDataReader(dir_path) | ||
else: | ||
raise ValueError(f"Unsupported mode: {mode}") | ||
|
||
class VideoDataReader(FrameDataReader): | ||
|
||
def __init__(self, video_path): | ||
self.video_path = video_path | ||
self.cap = cv.VideoCapture(video_path) | ||
if not self.cap.isOpened(): | ||
raise ValueError(f"Cannot open video file: {video_path}") | ||
|
||
|
||
def __iter__(self): | ||
return self | ||
|
||
def __next__(self): | ||
if self.cap.isOpened(): | ||
ret, frame = self.cap.read() | ||
if ret: | ||
return frame | ||
else: | ||
self.cap.release() | ||
raise StopIteration | ||
else: | ||
raise StopIteration | ||
|
||
class ImgDataReader(FrameDataReader): | ||
def __init__(self, dir_path): | ||
self.index = 0 | ||
self.directory_path = dir_path | ||
if not os.path.exists(dir_path): | ||
raise ValueError(f"Directory does not exist: {dir_path}") | ||
self.image_files = [ | ||
os.path.join(dir_path, f) for f in os.listdir(dir_path) | ||
if f.lower().endswith((".png", ".jpg", ".jpeg", ".bmp", ".tiff")) | ||
] | ||
|
||
def __iter__(self): | ||
return self | ||
|
||
def __next__(self): | ||
if self.index < len(self.image_files): | ||
img_path = self.image_files[self.index] | ||
self.index += 1 | ||
img = cv.imread(img_path) | ||
if img is None: | ||
raise ValueError(f"Cannot read image file: {img_path}") | ||
return img | ||
else: | ||
raise StopIteration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import cv2 as cv | ||
import random | ||
from abc import ABC, abstractmethod | ||
class Detector(ABC): | ||
@abstractmethod | ||
def detect(image): | ||
pass | ||
@staticmethod | ||
def create(mode): | ||
if mode == "vehicle": | ||
return VehicleDetector() | ||
elif mode == "fake": | ||
return FakeDetector() | ||
else: | ||
raise ValueError(f"Unsupported mode: {mode}") | ||
|
||
class VehicleDetector(Detector): | ||
def __init__(self): | ||
pass | ||
def detect(image): | ||
pass | ||
class FakeDetector(Detector): | ||
def __init__(self, seed = None): | ||
if seed is not None: | ||
random.seed(seed) | ||
|
||
@staticmethod | ||
def detect(image): | ||
if image is None or image.size == 0: | ||
return [] | ||
height, width = image.shape[:2] | ||
bboxes = [] | ||
num_boxes = random.randint(0, 7) | ||
|
||
for _ in range(num_boxes): | ||
if width < 2 or height < 2: | ||
continue | ||
cl = random.choice(["car", "bus", "truck"]) | ||
x1 = random.randint(0, width - 2) | ||
x2 = random.randint(x1 + 1, width - 1) | ||
|
||
y1 = random.randint(0, height - 2) | ||
y2 = random.randint(y1 + 1, height - 1) | ||
|
||
bboxes.append((cl, x1, y1, x2, y2)) | ||
|
||
return bboxes |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Это к вопросу об использовании init.py, который мы обсуждали после собрания. По смыслу в директории utils должен быть такой файлик, тогда можно писать стандартные импорты. Ниже примеры. Надо попробовать это реализовать.