Skip to content
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

Assignment #3 Submission - Alex Tretyakov and Adnan Abu Ramadan #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
# Practical Assignment 3
**Dealine**: 02.04.2021
**Deadline**: 02.04.2021

Please put your name here:
**Name:** .......
Please put your name(s) here:
**Name:** Alex Tretyakov and Adnan Abu Ramadan
## Problem 3.1
### Ring Buffer (Points 30)
1. Fork the current repository
2. Study the new framework-code of
2. Study the new framework-code of
- main.cpp
- Camera Controller.h/cpp
- Face.h/cpp
- Marker.h/cpp
3. Check that the code is running correctly: it should show the video stream from the web-camera of your laptop.
4. Implement the _ring buffer_ from lecture by modifying the code in Camera Controller.cpp file (places marked with MODIFY CODE HERE tag)
5. In cases when producer overwrites a frame, which was not querried by consumer, report a _drop frame_ state by printing the corresponding message to console. Test it with increasing the delay time in `waitKey()`function in the main loop of consumer.
5. In cases when producer overwrites a frame, which was not queried by consumer, report a _drop frame_ state by printing the corresponding message to console. Test it with increasing the delay time in `waitKey()`function in the main loop of consumer.

## Problem 3.2
### Face detection (Points 20)
Incorporate the face detection solution you done in the [Assignment 1](https://github.com/Jacobs-University/visir-tracker-01) into the framework in the following way:
1. Your face detector should return a vector of pointer to the `CFace` classes with detected faces: `std::vector<ptr_face_t> vpFaces`
2. Implment function `CMarker::markFaces()` and use it for drawing the faces to GUI
1. Your face detector should return a vector of pointer to the `CFace` classes with detected faces: `std::vector<ptr_face_t> vpFaces`
2. Implement function `CMarker::markFaces()` and use it for drawing the faces to GUI
3. Perform face detection every 10th frame.

## Problem 3.3
### Face tracking (Points 50)
Incorporate the optical flow field solution you done in the [Assignment 2](https://github.com/Jacobs-University/visir-tracker-02) into the framework in the following way:
1. Your sparse optical flow shoud return a vector of 2-d points: `std::vector<Point2f>`
2. Implment function `CMarker::markVecOFF()` and use it for drawing the optical flow field (feel free to modify its arguments if needed).
3. Now detect the points only in the _face area_ , whic is described in `vpFaces` variable.
1. Your sparse optical flow should return a vector of 2-d points: `std::vector<Point2f>`
2. Implement function `CMarker::markVecOFF()` and use it for drawing the optical flow field (feel free to modify its arguments if needed).
3. Now detect the points only in the _face area_ , which is described in `vpFaces` variable.
4. Update the detected points every 10th frame (when face detection is used).
5. Track also the points added with mouse in the mouse callback function.
6. Inbetween 10 frames (when the face detection is not applied) track the detected faces using the optical flow field. Update the pisition of faces in `vpFaces` variable variable for every frame.
6. In between 10 frames (when the face detection is not applied) track the detected faces using the optical flow field. Update the position of faces in `vpFaces` variable variable for every frame.

## Problem 3.4
### Graphical User Interface (GUI) (Bonus Points 20)
Expand All @@ -42,5 +42,5 @@ Please submit the assignment by making a pull request.
**Important** : Please make sure that
- No _extra files_ are submitted (except those, which were mentioned in the assignment)
- The changes were made _only_ in those files where you were asked to write your code
- The Continiouse Integration system (appVeyor) can build the submitted code
- The rendered images are also submitted in the folder "renders"
- The Continuous Integration system (appVeyor) can build the submitted code
- The rendered images are also submitted in the folder "renders"
176 changes: 93 additions & 83 deletions src/CameraController.cpp
Original file line number Diff line number Diff line change
@@ -1,83 +1,93 @@
#include "CameraController.h"

void CCameraCantroller::init()
{
if (!m_camera.open(0)) {
printf("Can't find a camera\n");
exit(1);
};
}

void CCameraCantroller::start()
{
m_terminate_producer = false;
m_thr_producer = std::thread([&]() {
Mat img;
for (;;) {
// ------ MODIFY CODE HERE -------
m_mtx_FrameBuffer.lock();
m_camera >> m_vFrameBuffer[pointer_in % m_vFrameBuffer.size()];
pointer_in++;
m_mtx_FrameBuffer.unlock();
m_inFrameCounter++;

std::this_thread::sleep_for(std::chrono::milliseconds(1));

if (m_terminate_producer) break;
}
});

const double tickFrequency = getTickFrequency();
m_terminate_counter = false;
m_thr_counter = std::thread([&, tickFrequency]() {
for (;;) {
static int64 ticks_old = getTickCount();
int64 ticks_new = getTickCount();
double sec = (ticks_new - ticks_old) / tickFrequency;
ticks_old = ticks_new;

static int64 in_fps_old = m_inFrameCounter;
int64 in_fps_new = m_inFrameCounter;
int64 in_fps = in_fps_new - in_fps_old;
in_fps_old = in_fps_new;

static int64 out_fps_old = m_outFrameCounter;
int64 out_fps_new = m_outFrameCounter;
int64 out_fps = out_fps_new - out_fps_old;
out_fps_old = out_fps_new;

printf("FPS: in: %.2f | out: %.2f\n", in_fps / sec, out_fps / sec);

std::this_thread::sleep_for(std::chrono::seconds(1));
if (m_terminate_counter) break;
}
});
}

Mat CCameraCantroller::getFrame()
{
Mat res;
if (pointer_out != pointer_in) {

// ------ MODIFY CODE HERE -------
m_mtx_FrameBuffer.lock();
if (!m_vFrameBuffer.empty()) {
res = m_vFrameBuffer[pointer_out % m_vFrameBuffer.size()];
pointer_out++;
}
m_mtx_FrameBuffer.unlock();
}
m_outFrameCounter++;
return res;
}

void CCameraCantroller::stop()
{
m_terminate_producer = true;
m_thr_producer.join();

m_terminate_counter = true;
m_thr_counter.join();
}


#include "CameraController.h"

void CCameraCantroller::init()
{
if (!m_camera.open(0)) {
printf("Can't find a camera\n");
exit(1);
};
}

void CCameraCantroller::start()
{
m_terminate_producer = false;
m_thr_producer = std::thread([&]() {
Mat img;
int size_of_buffer = m_vFrameBuffer.size();
for (;;) {
// ------ MODIFY CODE HERE -------
m_mtx_FrameBuffer.lock();
m_camera >> m_vFrameBuffer[pointer_in];

if(m_FrameBufferFull){
pointer_out = (pointer_out + 1) % size_of_buffer;
std::cout << "Frame Drop \n";
}
pointer_in = (pointer_in + 1) % size_of_buffer;
m_FrameBufferFull = (pointer_in == pointer_out) && (m_inFrameCounter != 0) && ((m_inFrameCounter - m_outFrameCounter) == size_of_buffer);

m_mtx_FrameBuffer.unlock();
m_inFrameCounter++;

std::this_thread::sleep_for(std::chrono::milliseconds(1));

if (m_terminate_producer) break;
}
});

const double tickFrequency = getTickFrequency();
m_terminate_counter = false;
m_thr_counter = std::thread([&, tickFrequency]() {
for (;;) {
static int64 ticks_old = getTickCount();
int64 ticks_new = getTickCount();
double sec = (ticks_new - ticks_old) / tickFrequency;
ticks_old = ticks_new;

static int64 in_fps_old = m_inFrameCounter;
int64 in_fps_new = m_inFrameCounter;
int64 in_fps = in_fps_new - in_fps_old;
in_fps_old = in_fps_new;

static int64 out_fps_old = m_outFrameCounter;
int64 out_fps_new = m_outFrameCounter;
int64 out_fps = out_fps_new - out_fps_old;
out_fps_old = out_fps_new;

printf("FPS: in: %.2f | out: %.2f\n", in_fps / sec, out_fps / sec);

std::this_thread::sleep_for(std::chrono::seconds(1));
if (m_terminate_counter) break;
}
});
}

Mat CCameraCantroller::getFrame()
{
Mat res;
int bufferSize = m_vFrameBuffer.size();
if (pointer_out != pointer_in) {

// ------ MODIFY CODE HERE -------
m_mtx_FrameBuffer.lock();
if (!m_vFrameBuffer.empty()) {
res = m_vFrameBuffer[pointer_out];
m_FrameBufferFull = false;
pointer_out = (pointer_out + 1) % bufferSize;
}
m_mtx_FrameBuffer.unlock();
}
m_outFrameCounter++;
return res;
}

void CCameraCantroller::stop()
{
m_terminate_producer = true;
m_thr_producer.join();

m_terminate_counter = true;
m_thr_counter.join();
}


45 changes: 23 additions & 22 deletions src/CameraController.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,33 @@

class CCameraCantroller {
public:
CCameraCantroller(size_t buf_len) : m_vFrameBuffer(buf_len) {}
~CCameraCantroller(void) = default;
CCameraCantroller(size_t buf_len) : m_vFrameBuffer(buf_len) {}
~CCameraCantroller(void) = default;

void init(void);
void start(void);
Mat getFrame(void);
void stop(void);
void init(void);
void start(void);
Mat getFrame(void);
void stop(void);


private:
VideoCapture m_camera;
std::deque<Mat> m_vFrameBuffer;
std::mutex m_mtx_FrameBuffer;

size_t m_inFrameCounter = 0;
size_t m_outFrameCounter = 0;
size_t pointer_in = 0;
size_t pointer_out = 0;

// Producer
std::thread m_thr_producer;
bool m_terminate_producer;

// Counter
std::thread m_thr_counter;
bool m_terminate_counter;
VideoCapture m_camera;
std::deque<Mat> m_vFrameBuffer;
std::mutex m_mtx_FrameBuffer;

size_t m_inFrameCounter = 0;
size_t m_outFrameCounter = 0;
size_t pointer_in = 0;
size_t pointer_out = 0;
bool m_FrameBufferFull = 0;

// Producer
std::thread m_thr_producer;
bool m_terminate_producer;

// Counter
std::thread m_thr_counter;
bool m_terminate_counter;


};
1 change: 0 additions & 1 deletion src/Face.cpp
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
#include "Face.h"

64 changes: 32 additions & 32 deletions src/Face.h
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
#pragma once
#include "types.h"
class CFace {
public:
CFace(void) : CFace(Rect()) {}
CFace(const Rect& area, int id = -1, const std::string& text = "unknown")
: m_area(area)
, m_id(id)
, m_text(text)
{}
~CFace(void) = default;
// Setters
void setID(int id) { m_id = id; }
void setText(const std::string& text) { m_text = text; }
void setArea(const Rect& area) { m_area = area; }
// Getters
int getID(void) const { return m_id; }
std::string getText(void) const { return m_text; }
Rect getArea(void) const { return m_area; }
private:
int m_id;
std::string m_text;
Rect m_area;
#pragma once

#include "types.h"

class CFace {
public:
CFace(void) : CFace(Rect()) {}
CFace(const Rect& area, int id = -1, const std::string& text = "unknown")
: m_area(area)
, m_id(id)
, m_text(text)
{}
~CFace(void) = default;

// Setters
void setID(int id) { m_id = id; }
void setText(const std::string& text) { m_text = text; }
void setArea(const Rect& area) { m_area = area; }

// Getters
int getID(void) const { return m_id; }
std::string getText(void) const { return m_text; }
Rect getArea(void) const { return m_area; }


private:
int m_id;
std::string m_text;
Rect m_area;
};
// Pointer
using ptr_face_t = std::shared_ptr<CFace>;

// Pointer
using ptr_face_t = std::shared_ptr<CFace>;
Loading